satishkumardhule
BAN USERInterviewer is asking to write code for unix utility "dirname"
- satishkumardhule February 25, 2015simple with complexity=n
package amazon.test.practice;
import java.util.*;
import java.io.*;
public class Wordcount {
public static void main(String args[]) throws FileNotFoundException
{
HashMap<String, Integer> hm = new HashMap<String, Integer>();
Scanner input=new Scanner(new File("c:\\1.txt"));
String max="none";
hm.put(max, 0);
while(input.hasNext())
{
String word = input.next();
//System.out.println(word);
if (hm.containsKey(word))
{
hm.put(word,hm.get(word)+1);
}
else
{
hm.put(word, 1);
}
if (hm.get(word)>hm.get(max))
{
max=word;
}
}
/* Set set = hm.entrySet();
Iterator i=set.iterator();
while(i.hasNext())
{
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}*/
System.out.println(max+"-------"+hm.get(max));
}
}
- satishkumardhule February 25, 2015