1const countOccurrences = arr => arr.reduce((prev, curr) => (prev[curr] = ++prev[curr] || 1, prev), {});
2
3// Examples
4countOccurrences([2, 1, 3, 3, 2, 3]); // { '1': 1, '2': 2, '3': 3 }
5countOccurrences(['a', 'b', 'a', 'c', 'a', 'b']); // { 'a': 3, 'b': 2, 'c': 1 }
1//Everyone was sharing complex answers. I'm just gonna put my simple-ish code here
2//It suuports multiple values of same frequency
3//If u want the first value and not multiple in case of multiple modes just add [0] after pop() function
4function checkMostFrequent(arr) {
5 var outArr = []; //output variable (an array containing arrays)
6 for (let i = 0; i < arr.length; i++) {
7 var pushed = false; //keep track
8 for (let j = 0; j < outArr.length; j++) {
9 if (!outArr[j].includes(arr[i])) { //for arrays include function checks for given elements
10 outArr[j].push(arr[i]); //push if array doesn't have the variable
11 pushed = true;
12 break;
13 }
14 }
15 if (!pushed) {
16 outArr.push([arr[i]]); //push a brand new array of array
17 }
18 }
19 return outArr.pop(); //return the last one (therefore most frequent) //for this case pop() is similar to outArr[outArr.length-1]
20}
21
22//The code can be modifed without much difficulty to return the index or just make frequency checks too
23
24Here is the shorthand version if you want to put it in minified stuff:
25
26function checkMostFrequent(a){var o=[];a.forEach(i=>{var p=0;o.forEach(j=>{if(!j.includes(i)&&!p){j.push(i);p=1;}});if(!p){o.push([i]);}});return o.pop();}