Dev Raj
Dev Raj

Reputation: 670

How to get Second Most repeated Word in String using Maps

I am trying to get the second most repeated word in the sentence.

eg:

String paraString = "This is a paragraph with multiple strings. Get the second most repeated word from the paragraph text and print the words with count".

Here 'the' is repeated for thrice and 'paragraph' & 'with' are repeated twice.

I need to print the second most repeated words 'paragraph' & 'with'.

Here is the program which I wrote to get the First Most Repeated Words.

public Set<String> getMostRepeatedWords(Map<String, Integer> sortedMap) {
    Set<String> mostRepeatedWords = new HashSet<String>();
    int mostrepeatedWord = Collections.max(sortedMap.values());
    for (Map.Entry<String, Integer> entry : sortedMap.entrySet()) {
        if (mostrepeatedWord == entry.getValue()) {
            mostRepeatedWords.add(entry.getKey());
        }
    }

    return mostRepeatedWords;
}

Please help me out.

The one option which I have is below. Let me know if there are any other ways.

int mostrepeatedWord = Collections.max(sortedMap.values())-1;

Upvotes: 0

Views: 2425

Answers (2)

Joop Eggen
Joop Eggen

Reputation: 109597

Following your solution So you have getMostRepeatedWords and now want the second most repeated words. In pseudo-code this would be:

Map<String, Integer> sortedMap = ...;
SortedMap<String, Integer> rest = new TreeMap<>(sortedMap);
rest.removeAll(getMostRepeatedWords(sortedMap));
Set<String> secondMostRepeatedWords = getMostRepeatedWords(rest);

Remove the most repeated words and then on the rest get the most repeated words.

More effort You could also copy the values, sort them decreasingly, and then take the second lesser value: with index > 0, and value lesser than first.

Upvotes: 0

Dici
Dici

Reputation: 25980

Here is an example of what you could do with Java 8 :

public List<String> getMostRepeatedWords(String s) {
    Map<String,Integer> map = new HashMap<>();
    String[] words  = s.split("\\s+");
    for (String word : words) 
        map.put(word,map.containsKey(word) ? map.get(word) + 1 : 0);

    List<Entry<String,Integer>> tmp = new ArrayList<>(map.entrySet());
    Collections.sort(tmp,(e1,e2) -> Integer.compare(e2.getValue(),e1.getValue()));

    return tmp.stream().map(e -> e.getKey()).collect(Collectors.toList());
}

This method computes the complete list of the words sorted by decreasing number of occurrences. If you don't need the whole list, you should rather store the entries of the map in an array and then apply a quickselect on it, with a custom Comparator. Let me know if you are interested and I'll go in further details.

Upvotes: 1

Related Questions