Remove a duplicate word sentence in an array

589 views java
5

Given a problem that determines the output of the word, length of each word, and the number of times the word repeats, I have the following code capable to determine the word and length of each word below:

String sentence;
String charSentence;
String[] wordOutput;

private void analyzeWords(String s) {
    String[] words = sentence.split(" ");
    wordOutput = new String[words.length];
    int[] repeats = new int[words.length];

    // Increment a single repeat
    for (int i = 0; i < words.length; i++) {

        repeats[i] = 1;

        // Increment when repeated.
        for (int j = i + 1; j < words.length - 1; j++) {
            if (words[i].equalsIgnoreCase(words[j])) {
                repeats[i]++;
            }
        }

        wordOutput[i] = words[i] + "\t" + words[i].length() + "\t" + repeats[i];
    }

When I run the program, I get the following output:

Equal   5   2
Equal   5   1 <- This is a duplicate word and should not be here when it repeats.

Does anyone know where my problem is? Is it something relating that deals with my repeats array?

answered question

Are you allowed to use a HashMap? If so I would use this to record the word as the key and the number of occurrences as the value

I am not allowed to use HashMap as the problem doesn't require it.

One problem is that you set j to i+1, and then loop while it is less than the length minus one. You need to loop until the length. Next, you'll need some way to keep track of if a word has already occurred in the String

2 Answers

6

Instead of incrementing count at all index store the count only in last occurence of word, in other case, have the count value of 0. at the end traverse the count array, if its greater than zero, print the value and its count

private void analyzeWords(String s) {
    String[] words = sentence.split(" ");
    wordOutput = new String[words.length];
    int[] repeats = new int[words.length];
    for (int i = 0; i < words.length; i++) {
        int count =1;
        int index = i;
        for (int j = i + 1; j < words.length - 1; j++) {
            if (words[i].equalsIgnoreCase(words[j])) {
                count++;
                index = j;
            }
        }
        if(repeats[index]==0){
          repeats[index]=count; // update repeat array only for last occurence of word
          wordOutput[i] = words[i] + "\t" + words[i].length() + "\t" + repeats[index];
        }
    }

posted this
9

The first problem is that in the inner for loop you are looping from i+1 to length-1. You need to loop till length. Second you will need to determine if there are any occurrences of the word in the String, and if so use a continue statement. You can do:

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

    repeats[i] = 1;
    for(int index = i-1; index >= 0; index--) {
        if(words[i].equals(words[index])) {
            continue outer;
        }
    }
    ...
}

However the problem with this is that there will be null values at the end of the list, as you specify an Array with the same length as the number of words. To solve this you can do:

 wordOutput = Arrays.stream(wordOutput).filter(e-> e!= null).toArray(String[]::new);

Which will filter out the null values

Output:

(With the input String: "This is a String is a with a lot lot of this repeats repeats")

This    4   2
is      2   2
a       1   3
String  6   1
with    4   1
lot     3   2
of      2   1
this    4   1
repeats 7   2

posted this

Have an answer?

JD

Please login first before posting an answer.