Skip to content
Advertisement

Counting words using parallel Java arrays

I am trying to write code to count the frequency of each word in an array. I am wondering if I need to use two nested loops to keep track of both the array with integers and the array with the words. I have been fiddling with the code for hours and cannot make any progress. If you have any pointers, they would be much appreciated. I am given the input as first the number of elements in the array, then the words that I am supposed to count the frequency of, such as 4 dog cat dog fish.

import java.util.Scanner;

public class LabProgram {
    public static int getFrequencyOfWord(String[] wordsList, int listSize, String currWord) {
        int i;
        int count;
        int[] countArr = new int[listSize];
        // need to use two arrays; one for frequency, one for words.
        for (i = 0; i < countArr.length; ++i) {
            if (wordsList[i].compareTo(currWord) == 0) {
                countArr[i] += 1;
            }
        }
        //check previous LAB that had same concept; then make it use a method.
        return countArr[i];
    }

    public static void main(String[] args) {
        int size;
        int i;
        size = scnr.nextInt();
        String[] array = scnr.nextLine().split(" ");
        for (i = 0; i < array.length; ++i) {
            System.out.println(array[i]);
        }
        for (i = 0; i < array.length; ++i) {
            currWord = array[i];
            System.out.println(currWord + getFrequencyOfWord(array, size, currWord));
        }
    }
}

Advertisement

Answer

Can you please try below solution?:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class LabProgram {
    public static void main(String[] args) {
        try (Scanner scnr = new Scanner(System.in)) {
            int size = scnr.nextInt();
            Map<String, Integer> wordCounts = new HashMap<>();

            for (int i = 0; i < size; i++) {
                String s = scnr.next();
                if (wordCounts.containsKey(s)) {
                    int count = wordCounts.get(s);
                    wordCounts.put(s, ++count);
                } else {
                    wordCounts.put(s, 1);
                }
            }
            wordCounts.entrySet().stream().forEach(s -> System.out.println(
                    s.getKey() + ": " + s.getValue()));
        }
    }
}

Using Java 8 Streams:

import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class LabProgram {
    public static void main(String[] args) {
        try (Scanner scnr = new Scanner(System.in)) {
            int size = scnr.nextInt();
            String array[] = new String[size];

            for (int i = 0; i < size; i++) {
                array[i] = scnr.next();
            }

            Map<Object, Integer> data = Stream.of(array)
                    .collect(Collectors.groupingBy(
                            s -> s, Collectors.summingInt(s -> 1)));

            data.entrySet().stream().forEach(s -> System.out.println(
                    s.getKey() + ": " + s.getValue()));
        }
    }
}

If your input is:

4 dog cat dog fish

Output:

cat: 1
fish: 1
dog: 2
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement