## 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;
}

1. public String[] wordsWithout(String[] words, String target) {
ArrayList list = new ArrayList();

for (int i = 0; i < words.length; i++)

return (String[])list.toArray(new String[list.size()]);
}

2. public String[] wordsWithout(String[] words, String target) {
int 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;
}

3. public String[] wordsWithout(String[] words, String target) {
return Arrays.stream(words)
.filter(n -> !n.equals(target))
.toArray(String[]::new);
}

4. Easiest and simplest way via Java 8 streams:

return java.util.Arrays.stream(words).filter(x -> !x.equals(target)).toArray(String[]::new);

1. @EDUCATED STUDENT
Where to train Java8?

5. public String[] wordsWithout(String[] words, String target) {
int 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;
}

1. I need to find a faster and more efficient way to do this. Because I did the for loop twice which is not good.

6. public String[] wordsWithout(String[] words, String target) {
ArrayList list = new ArrayList ();
for(String str : words ){
}
String [] arr = list.toArray(new String[list.size()]);
return arr;
}