Second Most Repeated Word in an Array of Strings
secFrequent :
A function to find the second most repeated word in the array. After counting occurrences with a Map, it sorts these counts using an insertion sort algorithm. The function then matches the second highest frequency back to its corresponding word, demonstrating a blend of mapping, sorting, and searching techniques for array analysis.
1function insertionSort(arr) {
2  for (let i = 1; i < arr.length; i++) {
3    let current = arr[i];
4    let j = i - 1;
5    while (arr[j] > current && j >= 0) {
6      arr[j + 1] = arr[j];
7      j--;
8    }
9    arr[j + 1] = current;
10  }
11  return arr;
12}
13
14function secFrequent(arr) {
15  let m = new Map();
16  for (let i = 0; i < arr.length; i++) {
17    m.set(arr[i], (m.get(arr[i]) ?? 0) + 1);
18  }
19  let sortedOccurences = insertionSort([...m.values()]);
20  for (const key of m.keys()) {
21    if (m.get(key) === sortedOccurences[sortedOccurences.length - 2]) {
22      return key;
23    }
24  }
25
26  return "No repeated occurrence";
27}
28console.log(secFrequent(strArray));
29