# Second most repeated word in a sequence

Given a sequence of strings, the task is to find out the second most repeated (or frequent) string in the given sequence.(Considering no two words are the second most repeated, there will be always a single word).

Examples:

```Input : {"aaa", "bbb", "ccc", "bbb",
"aaa", "aaa"}
Output : bbb

Input : {"geeks", "for", "geeks", "for",
"geeks", "aaa"}
Output : for
```

1. Store all the words in a map with their occurrence with word as key and its occurrence as value.
2. Find the second largest value in the map.
3. Traverse the map again and return the word with occurrence value equals to second max value.

## C++

 `// C++ program to find out the second ` `// most repeated word ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find the word ` `string secMostRepeated(vector seq) ` `{ ` ` `  `    ``// Store all the words with its occurrence ` `    ``unordered_map occ; ` `    ``for` `(``int` `i = 0; i < seq.size(); i++) ` `        ``occ[seq[i]]++; ` ` `  `    ``// find the second largest occurrence ` `    ``int` `first_max = INT_MIN, sec_max = INT_MIN; ` `    ``for` `(``auto` `it = occ.begin(); it != occ.end(); it++) { ` `        ``if` `(it->second > first_max) { ` `            ``sec_max = first_max; ` `            ``first_max = it->second; ` `        ``} ` ` `  `        ``else` `if` `(it->second > sec_max &&  ` `                 ``it->second != first_max) ` `            ``sec_max = it->second; ` `    ``} ` ` `  `    ``// Return string with occurrence equals ` `    ``// to sec_max ` `    ``for` `(``auto` `it = occ.begin(); it != occ.end(); it++) ` `        ``if` `(it->second == sec_max) ` `            ``return` `it->first; ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``vector seq = { ``"ccc"``, ``"aaa"``, ``"ccc"``, ` `                          ``"ddd"``, ``"aaa"``, ``"aaa"` `}; ` `    ``cout << secMostRepeated(seq); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find out the second ` `// most repeated word ` ` `  `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` `    ``// Method to find the word ` `    ``static` `String secMostRepeated(Vector seq) ` `    ``{ ` `        ``// Store all the words with its occurrence ` `        ``HashMap occ = ``new` `HashMap(seq.size()){ ` `            ``@Override` `            ``public` `Integer get(Object key) { ` `                 ``return` `containsKey(key) ? ``super``.get(key) : ``0``; ` `            ``} ` `        ``}; ` `        `  `        ``for` `(``int` `i = ``0``; i < seq.size(); i++) ` `            ``occ.put(seq.get(i), occ.get(seq.get(i))+``1``); ` `      `  `        ``// find the second largest occurrence ` `       ``int` `first_max = Integer.MIN_VALUE, sec_max = Integer.MIN_VALUE; ` `         `  `       ``Iterator> itr = occ.entrySet().iterator(); ` `       ``while` `(itr.hasNext())  ` `       ``{ ` `           ``Map.Entry entry = itr.next(); ` `           ``int` `v = entry.getValue(); ` `           ``if``( v > first_max) { ` `                ``sec_max = first_max; ` `                ``first_max = v; ` `            ``} ` `      `  `            ``else` `if` `(v > sec_max &&  ` `                     ``v != first_max) ` `                ``sec_max = v; ` `       ``} ` `        `  `       ``// Return string with occurrence equals ` `        ``// to sec_max ` `       ``itr = occ.entrySet().iterator(); ` `       ``while` `(itr.hasNext())  ` `       ``{ ` `           ``Map.Entry entry = itr.next(); ` `           ``int` `v = entry.getValue(); ` `           ``if` `(v == sec_max) ` `                ``return` `entry.getKey(); ` `       ``} ` `        `  `       ``return` `null``; ` `    ``} ` `     `  `    ``// Driver method ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``String arr[] = { ``"ccc"``, ``"aaa"``, ``"ccc"``, ` `                         ``"ddd"``, ``"aaa"``, ``"aaa"` `}; ` `        ``List seq =  Arrays.asList(arr); ` `         `  `        ``System.out.println(secMostRepeated(``new` `Vector<>(seq))); ` `    ``}    ` `} ` `// This program is contributed by Gaurav Miglani `

Output:

```ccc
```