JSFiddle - React, Tailwind, and code Playground
JavaScript
arr1 = [];
arr2 = [];
for(i=0; i<50000; i++) {
arr1[i] = Math.floor(Math.random() * 50)
arr2[i] = arr1[i]
}
function mine(arr){
arr.sort(function(a, b){return a-b})
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)
}
console.time("mine")
mine(arr1)
console.timeEnd("mine")
console.time("theirs")
theirs(arr2)
console.timeEnd("theirs")