Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
highestFrequencyString-2
Write a function named `highestFrequencyString` that takes in an array of strings and returns the string with the highest frequency in the array and how many times it appears.
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser:
Chrome 130
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
METHOD MANIA
404238.9 Ops/sec
FOR LOOP MANIA
428746.8 Ops/sec
Tests:
METHOD MANIA
const highestFrequencyString = (strArray) => { // so janky can we do better? let res = {}; strArray.forEach((str) => (res[str]) ? res[str]++ : res[str]=1); // var reuse because i'm a degenerate res = Object.entries(res).sort((a,b) => b[1]-a[1])[0]; console.log(`${res[0]} appears ${res[1]} times in the array.`); } const fruits = ["apple", "orange", "apple", "peach", "pear", "apple", "peach"]; highestFrequencyString(fruits);
FOR LOOP MANIA
const highestFrequencyString = (strArray) => { strArray.sort((a,b) => a <= b ? -1 : 1); let max = 0; let curr = 0; let res; for (let i = 0; i < strArray.length-1; i++) { if (strArray[i] === strArray[i+1]) { curr++; } else { if (max < ++curr) { max = curr; res = strArray[i]; } curr = 0; } } console.log( `${res} appears ${max} times in the array.`); } const fruits = ["apple", "orange", "apple", "peach", "pear", "apple", "peach"]; highestFrequencyString(fruits);