school/java/old/word-freq/wordFreq.java

45 lines
840 B
Java
Raw Permalink Normal View History

2021-01-20 06:57:22 -05:00
import java.util.Scanner;
/*
* input: 5 hey hi Mark hi mark
* output:
* hey 1
* hi 2
* Mark 1
* hi 2
* mark 1
*
* assume input will always be less than 20 words
* hint: Use two arrays, one array for strings
* one array for frequencies
*/
public class wordFreq {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String[] words = new String[20];
int[] frequencies = new int[20];
int numberOfWords = scnr.nextInt();
for (int i=0; i<numberOfWords; i++){
words[i] = scnr.next();
}
for (int i=0; i<numberOfWords; i++){
for (int j=0; j<numberOfWords; j++){
if (words[i].equals(words[j])){
frequencies[i]++;
}
}
}
for (int i=0; i<numberOfWords; i++){
System.out.println(words[i] + " " + frequencies[i]);
}
scnr.close();
}
}