-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSecondMostRepeatingWord.js
54 lines (51 loc) · 1.46 KB
/
SecondMostRepeatingWord.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* @author Anirudh Sharma
*
* Given a sequence of strings, the task is to find out the second most repeated (or frequent)
* string in the given sequence.
*
* Note: No two strings are the second most repeated, there will be always a single string.
*
* Constraints:
* 1<=N<=10^3
*/
const secondMostFrequent = (strings) => {
// Map to store the frequencies of strings
// in the array
const frequencyMap = new Map();
for (let s of strings) {
if (frequencyMap.get(s)) {
let count = frequencyMap.get(s);
frequencyMap.set(s, count + 1);
} else {
frequencyMap.set(s, 1);
}
}
// Max and second max
let max = Number.MIN_VALUE;
let secondMax = Number.MIN_VALUE;
// Loop through the map to find the second map value
for(let [, v] of frequencyMap) {
if (max < v) {
secondMax = max;
max = v;
} else if (v > secondMax && v !== max) {
secondMax = v;
}
}
// Again iterate through the map to get the key
// corresponding to the the second max value
for (let [k, v] of frequencyMap) {
if (v === secondMax) {
return k;
}
}
return null;
};
const main = () => {
let strings = ["aaa", "bbb", "ccc", "bbb", "aaa", "aaa"];
console.log(secondMostFrequent(strings));
strings = ["geek", "for", "geek", "for", "geek", "aaa"];
console.log(secondMostFrequent(strings));
};
main();