JSFiddle - React, Tailwind, and code Playground
JavaScript
var arr = [3, 6, 6, 1, 5, 8, 9, 6, 6];
arr.sort();
function mine(a) {
var count = 0;
var integer = 0;
var tempCount = 1;
var tempInteger = 0;
var prevInt = null
for (var i = 0; i < arr.length; i++) {
tempInteger = arr[i]
if (i > 0) {
prevInt = arr[i - 1]
}
if (prevInt == arr[i]) {
tempCount += 1
if (tempCount > count) {
count = tempCount
integer = tempInteger
}
} else {
tempCount = 1
}
}
console.log("most repeated is: " + integer)
}
function theirs(a) {
var count = 1,
tempCount;
var popular = a[0];
var temp = 0;
for (var i = 0; i < (a.length - 1); i++) {
temp = a[i];
tempCount = 0;
for (var j = 1; j < a.length; j++) {
if (temp == a[j]) tempCount++;
}
if (tempCount > count) {
popular = temp;
count = tempCount;
}
}
console.log("most repeated is: " + popular)
}
function better(a) {
var top = a[0],
count = 0,
i = len = a.length - 1;
while (i--) {
var j = len,
temp = 0;
while (j--) {
if (a[j] == a[i]) ++temp;
}
if (temp > count) {
count = temp;
top = a[i];
}
}
console.log("most repeated is " + top);
}
console.clear();
console.time("mine");
for (var i = 0; ++i <= 500;) mine(arr);
console.timeEnd("mine")
console.time("theirs")
for (var i = 0; ++i <= 500;) theirs(arr)
console.timeEnd("theirs")
console.time("better")
for (var i = 0; ++i <= 500;) better(arr)
console.timeEnd("better")