Java > AP-1 > wordsWithout (CodingBat Solution)

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


12 comments :

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

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

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

    ReplyDelete
  4. Easiest and simplest way via Java 8 streams:

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

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

    ReplyDelete
    Replies
    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.

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

    ReplyDelete
  7. public class WordsWithoutList {
    public 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;
    }
    }

    ReplyDelete

  8. public 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;
    }

    ReplyDelete
  9. Here is mine:
    public 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);
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact