Ali Gajani
Ali Gajani

Reputation: 15091

How can I count the frequency of words without using Collections?

Results

0: Bear
1: Car
2: Bear
3: Cat
4: Car
5: Dog
6: Bear
---Frequency---
Bear : 1
Car : 1
null : 1
Cat : 1
null : 1
Dog : 1
null : 1

Code

import java.util.Arrays;
import java.util.StringTokenizer;

public class WordCount {

    public static void main(String[] args) {

        String text = "Bear Car Bear Cat Car Dog Bear";
        StringTokenizer str = new StringTokenizer(text);
        String word[] = new String[10];
        String unique[] = new String[10];
        String w;

        int count = -1;

        while (str.hasMoreTokens()) {
            count++;
            w = str.nextToken();
            word[count] = w;

            System.out.println(count + ": " + word[count]);

        }

        System.out.println("---Frequency---");

        // create unique words
        for (int i = 0; i < 7; i++) {

            if ((!Arrays.asList(unique).contains(word[i]))) {
                unique[i] = word[i];
            }
        }

        // measuring frequency
        int[] measure = new int[10];

        for (int z = 0; z < 7; z++) {
            if (Arrays.asList(unique).contains(word[z])) {
                measure[z] += 1;
                System.out.println(unique[z] + " : " + measure[z]);
            }
        }
    }
}

Upvotes: 1

Views: 2756

Answers (3)

Ashot Karakhanyan
Ashot Karakhanyan

Reputation: 2830

Another solution:

String text = "Bear Car Bear Cat Car Dog Bear";
String[] allWords = text.split(" ");
String[] foundWords = new String[allWords.length];
int[] foundCount = new int[allWords.length];
int foundIndex= 0;

for (String aWord : allWords) {
    int j = 0;
    for (; j < foundIndex; j++) {
        if (foundWords[j].equals(aWord)) { //found
            foundCount[j]++;
            break;
        }
    }
    if (j == foundIndex) { //word bot found in foundWords
        foundWords[foundIndex] = aWord;
        foundCount[foundIndex] = 1;
        foundIndex++;
    }
}

// Print result
for (int i = 0; i <foundIndex ; i++) {
    System.out.println(foundWords[i] + " : " + foundCount[i]);
}

The result is:

Bear : 3
Car : 2
Cat : 1
Dog : 1

Upvotes: 1

Ali Gajani
Ali Gajani

Reputation: 15091

Thanks to Luiggi for the inspiration . Here is my solution, realizing I had missed something very important. The nested loops. It is just a few lines of editing to my existing code. I would love you all to look at Luiggi's code as it is more verbose (hehe).

Result

0: Bear
1: Car
2: Bear
3: Cat
4: Car
5: Dog
6: Bear
---Frequency---
Bear : 3
Car : 2
Cat : 1
Dog : 1

import java.util.Arrays;
import java.util.StringTokenizer;

public class WordCount {

    public static void main(String[] args) {

        String text = "Bear Car Bear Cat Car Dog Bear";
        StringTokenizer str = new StringTokenizer(text);
        String word[] = new String[10];
        String unique[] = new String[10];
        String w;

        int count = -1;

        while (str.hasMoreTokens()) {
            count++;
            w = str.nextToken();
            word[count] = w;

            System.out.println(count + ": " + word[count]);

        }

        System.out.println("---Frequency---");

        // create unique words
        for (int i = 0; i < 7; i++) {

            if ((!Arrays.asList(unique).contains(word[i]))) {

                unique[i] = word[i];
            }

        }

        // measuring frequency
        int[] measure = new int[10];

        for (int z = 0; z < 7; z++) {

            if (unique[z] != null) {

                for (int j = 0; j < 7; j++) {

                    if (unique[z].equals(word[j])) {

                        measure[z] += 1;

                    }
                }
                System.out.println(unique[z] + " : " + measure[z]);
            }

        }

    }

}

Upvotes: 1

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

There are several problems in your current code:

  1. You're using Collection interface explicitly. This is done every time you call Arrays#asList(...). You're not fulfilling with your main requirement.
  2. You should maintain a counter for the elements in all your arrays. In this case, you could use the same variable to hold the size of both unique and measure arrays.
  3. Your algorithm to fill unique is wrong. You should only add the word once (since it must be unique).
  4. You should add the counter for measure only once per every String in unique array.

This code takes into account all these recommendations.

import java.util.StringTokenizer;

public class ThirteenthMain {

    public static void main(String[] args) {

        String text = "Bear Car Bear Cat Car Dog Bear";
        StringTokenizer str = new StringTokenizer(text);
        String word[] = new String[10];
        String unique[] = new String[10];
        // reading the words to analyze
        int wordSize = 0;
        while (str.hasMoreTokens()) {
            String w = str.nextToken();
            word[wordSize] = w;
            System.out.println(wordSize + ": " + word[wordSize]);
            wordSize++;
        }
        System.out.println("---Frequency---");
        // create unique words
        int uniqueWordSize = 0;
        for (int i = 0; i < wordSize; i++) {
            boolean found = false;
            for (int j = 0; j < uniqueWordSize; j++) {
                if (word[i].equals(unique[j])) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                unique[uniqueWordSize++] = word[i];
            }
        }
        // measuring frequency
        int[] measure = new int[10];
        for (int i = 0; i < uniqueWordSize; i++) {
            for (int j = 0; j < wordSize; j++) {
                if (unique[i].equals(word[j])) {
                    measure[i]++;
                }
            }
        }
        //printing results
        for (int i = 0; i < uniqueWordSize; i++) {
            System.out.println(unique[i] + " : " + measure[i]);
        }
    }
}

It prints:

0: Bear
1: Car
2: Bear
3: Cat
4: Car
5: Dog
6: Bear
---Frequency---
Bear : 3
Car : 2
Cat : 1
Dog : 1

Upvotes: 2

Related Questions