import java.util.*;
import java.lang.*;
import java.io.*;

class Main {
    public static int [] countFrequencies(String [] words){
        ArrayList<String> wordList = new ArrayList<>();
        ArrayList<Integer> frequenciesList = new ArrayList<>();
        Arrays.sort(words);

        for(int i=0; i<words.length;i++){
            //System.err.println(words[i]);
            int sum=0;
            if(!wordList.contains(words[i])){
                wordList.add(words[i]);
                sum+=1;
                for(int j=i+1; j<words.length;j++){
                    if(words[j].equals(words[i]))
                        sum+=1;
                }
                frequenciesList.add(sum);
            }
        }
        //System.err.println(frequencies);
        int[] frequencies = new int[wordList.size()];
        for(int i=0; i<frequencies.length; i++)
            frequencies[i] = frequenciesList.get(i);
        return frequencies;
    }

    public static void main(String[] args) {
        int [] frequencies = countFrequencies(new String[]{"the", "dog", "got", "the", "bone"});
        for(int frequence : frequencies)
            System.out.println(frequence);
    }
    
    /*Implement the method countFrequencies (words) which takes as input an array of strings (words), representing a tokenized word document. For example:
    ['the', 'dog', 'got', 'the', 'bone']
    Your countFrequencies method should return an array of integers containing the number of occurrences of each word sorted alphabetically.
    For the above example, the correct output would be:
    # bone = 1, dog = 1, got = 1, the = 2*/
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: