Skip to content

Commit

Permalink
day 158
Browse files Browse the repository at this point in the history
  • Loading branch information
jwardoin committed Aug 30, 2022
1 parent 5084a6e commit 83e9c39
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions highestRankNumInArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Complete the method which returns the number which is most frequent in the given input array. If there is a tie for most frequent number, return the largest number among them.

// Note: no empty arrays will be given.

// Examples
// [12, 10, 8, 12, 7, 6, 4, 10, 12] --> 12
// [12, 10, 8, 12, 7, 6, 4, 10, 12, 10] --> 12
// [12, 10, 8, 8, 3, 3, 3, 3, 2, 4, 10, 12, 10] --> 3

// my original solution

function highestRank(arr) {
let highest = 0;
const table = arr.reduce((a, b) => {
if (!a[b]) {
a[b] = 0;
}
a[b]++;
return a;
}, {});

const entries = Object.entries(table);

entries.forEach((answer, i, arr) => {
console.log(answer);
if (answer[1] >= arr[highest][1] && +answer[0] > +arr[highest][0]) {
highest = i;
}
});
return +entries[highest][0];
}

0 comments on commit 83e9c39

Please sign in to comment.