Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function findMostFrequent(arr) {
- var frequency = 1;
- var bestFrequency = 1;
- var element;
- for (var i = 0; i < arr.length; i++) {
- frequency = 1;
- for (var j = 0; j < arr.length; j++) {
- if (arr[i] === arr[j]) {
- frequency++;
- if (frequency > bestFrequency) {
- bestFrequency = frequency;
- element = arr[i];
- }
- }
- }
- }
- return element + " " + frequency;
- }
- console.log(findMostFrequent([1, 2, 1, 4, 6, 1, 2, 4]));
Advertisement
Add Comment
Please, Sign In to add comment