Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
highestFrequencyString
(version: 0)
1. 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.
Comparing performance of:
METHOD MANIA vs FOR LOOP MANIA
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
METHOD MANIA
const fruits = ["apple", "orange", "apple", "peach", "pear", "apple", "peach"] console.log(highestFrequencyString(fruits)); // "apple appears 3 times in the array." const highestFrequencyString = (strArray) => { 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]; return `${res[0]} appears ${res[1]} times in the array.` }
FOR LOOP MANIA
const fruits = ["apple", "orange", "apple", "peach", "pear", "apple", "peach"] console.log(highestFrequencyString(fruits)); // "apple appears 3 times in the array." 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 { curr++; if (max < curr) { max = curr; res = strArray[i]; } curr = 0; } } return `${res} appears ${max} times in the array.` }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
METHOD MANIA
FOR LOOP MANIA
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Comments
Confirm delete:
Do you really want to delete benchmark?