Skip to content

Instantly share code, notes, and snippets.

@McLarenCollege
Last active October 6, 2022 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McLarenCollege/37b1c34a6fa69fccac513141b51c109d to your computer and use it in GitHub Desktop.
Save McLarenCollege/37b1c34a6fa69fccac513141b51c109d to your computer and use it in GitHub Desktop.
Most Frequent Element in an Array

Create a function that takes an array and returns a new array containing the most frequently occuring elements in it.

function mostFrequentElement(arr){
// write your code here
}
console.log(mostFrequentElement([1,2,5,1,3,5,2,1])); // [1]
console.log(mostFrequentElement([1,2,5,1,3,5,2,1,2])); // [1,2]
console.log(mostFrequentElement([1,2,5,1,3,5,2,1,2,3,3])); // [1,2,3]

Please watch this video for a hint for the problem:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment