By lokeshgupta1981
via howtodoinjava.com
Published: Nov 11 2012 / 09:37
This is very common interview question where you have to find duplicate words in a string or some text. This can be solved using some overly-complex algorithms also, but in this post, I will propose rather an easy way using java collections.
Comments
htowninsomniac replied ago:
As interviewer, my next question would be "great, now assume Collections.frequency didn't exist. How would you do it then?" And strictly speaking, your answer still needs to eliminate words that have frequency 1.
lokeshgupta1981 replied ago:
You can ask. You are the boss... Let me make a wild guess. In my other simplest solution, I will sort the collection so that it makes similar words in sequence. Now a simple iteration will filter out all different words and their frequency.. including one also. You can choose them to ignore. I know this solution can have some performance overhead, but for that large load of data, I will go for some expert algorithm implementation. Otherwise, it is also good to go.
Voters For This Link (3)
Voters Against This Link (0)