Hey guys everything is fine with my code it works and is verified succesfully.
I am posting this so that you guys could suggest me any further modifications in my code(i.e if there is some unnecessary code, or may be i could have written some logic better etc).
Go through my code and suggest me if any.
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<>();
int[] count = new int[list.size()];
for (int i = 0; i < list.size(); i++){
result.put(list.get(i), count[i]);
}
Iterator<Map.Entry<String, Integer>> iterator = result.entrySet().iterator();
while (iterator.hasNext())
{
Map.Entry<String, Integer> pair = iterator.next();
String key = pair.getKey();
Integer value = pair.getValue();
for (String i : list){
if (i.equals(key)){
value++;
result.replace(key,value);
}
}
}
while (iterator.hasNext())
{
Map.Entry<String, Integer> pair = iterator.next();
String key = pair.getKey();
Integer value = pair.getValue();
if (key.equals(iterator.next().getKey())){
value++;
result.replace(key,value);
}
}
return result;
}
}