Problem:
Given an array of strings, return a new array without the strings that are equal to the target string. One approach is to count the occurrences of the target string, make a new array of the correct length, and then copy over the correct strings.
wordsWithout({"a", "b", "c", "a"}, "a") → {"b", "c"}
wordsWithout({"a", "b", "c", "a"}, "b") → {"a", "c", "a"}
wordsWithout({"a", "b", "c", "a"}, "c") → {"a", "b", "a"}
Solution:
public String[] wordsWithout(String[] words, String target) { int found = 0; for (int i = 0; i < words.length; i++) { if (words[i].equals(target)) found++; } found = words.length - found; int place = 0; String[] str = new String[found]; for (int j = 0; j < words.length; j++) { if (!words[j].equals(target)) { str[place] = words[j]; place++; } } return str; }
public String[] wordsWithout(String[] words, String target) {
ReplyDeleteArrayList list = new ArrayList();
for (int i = 0; i < words.length; i++)
if (words[i] != target) list.add(words[i]);
return (String[])list.toArray(new String[list.size()]);
}
Nice!!!
Deletepublic String[] wordsWithout(String[] words, String target) {
ReplyDeleteint count=0;
for (int i = 0; i < words.length; i++) {
if(words[i]!=target){
count++;
}
}
int chk=0;
String[] arr=new String[count];
for (int i = 0; i < words.length; i++) {
if(words[i]!=target){
arr[chk]=words[i];
chk++;
}
}
return arr;
}
public String[] wordsWithout(String[] words, String target) {
ReplyDeletereturn Arrays.stream(words)
.filter(n -> !n.equals(target))
.toArray(String[]::new);
}
Easiest and simplest way via Java 8 streams:
ReplyDeletereturn java.util.Arrays.stream(words).filter(x -> !x.equals(target)).toArray(String[]::new);
@EDUCATED STUDENT
DeleteWhere to train Java8?
public String[] wordsWithout(String[] words, String target) {
ReplyDeleteint len = words.length;
int counter = 0;
for(int i=0; i<len; i++) {
if(words[i].equals(target)) {
continue;
}
else {
counter++;
}
}
String[] ar = new String[counter];
int b = 0;
for(int i=0; i<len; i++) {
if(words[i].equals(target)) {
continue;
}
else {
ar[b] = words[i];
b++;
}
}
return ar;
}
I need to find a faster and more efficient way to do this. Because I did the for loop twice which is not good.
Deletepublic String[] wordsWithout(String[] words, String target) {
ReplyDeleteArrayList list = new ArrayList ();
for(String str : words ){
if( !(str.equals(target)) ) list.add(str);
}
String [] arr = list.toArray(new String[list.size()]);
return arr;
}
public class WordsWithoutList {
ReplyDeletepublic List wordsWithoutList(String[] words, int len) {
final List list = new ArrayList<>(words.length);
for (final String s: words){
if(s.length() != len) {
list.add(s);
}
}
return list;
}
}
ReplyDeletepublic List wordsWithoutList(String[] words, int len) {
final List list = new ArrayList<>(words.length);
for (final String s: words){
if(s.length() != len) {
list.add(s);
}
}
return list;
}
Here is mine:
ReplyDeletepublic String[] wordsWithout(String[] words, String target) {
ArrayList list = new ArrayList<>();
for (int i = 0; i < words.length; i++){
list.add(words[i]);
if (words[i] == target){
list.remove(words[i]);
}
}
String[] arr = new String[list.size()];
return list.toArray(arr);
}