The problem is with first name it doesnt count. By the way there are only 10 inputs because i am testing the code with 10 inputs first because if it works with 10 entries then it should work with 20 entries too as per conditions.
Deepak
Dheeraj
Deepak
Deepak
Deepak
Tim
Jim
Tim
Jack
Jim
Dheeraj 1
Tim 2
Jack 1
Deepak 3
Jim 2
package com.codegym.task.task10.task1016;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/*
Identical words in a list
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> words = new ArrayList<>();
for (int i = 0; i < 20; i++) {
words.add(reader.readLine());
}
Map<String, Integer> map = countWords(words);
for (Map.Entry<String, Integer> pair : map.entrySet()) {
System.out.println(pair.getKey() + " " + pair.getValue());
}
}
public static Map<String, Integer> countWords(ArrayList<String> list) {
HashMap<String, Integer> result = new HashMap<String, Integer>();
int[] count = new int[list.size()];
for (int i = 0; i < list.size(); i++){
for (int j = 1; j < list.size(); j++){
if (list.get(i).equalsIgnoreCase(list.get(j))){
count[i]++;
result.put(list.get(i), count[i]);
}
// else {
// count[i]++;
// result.put(list.get(i), count[i]);
// }
}
}
return result;
}
}