Created
August 14, 2022 07:01
-
-
Save wjuniorw/34a8afbde3d92dfdc52055508b94e576 to your computer and use it in GitHub Desktop.
find most recurrent number...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const arr = [1, 2, 3, 4, 1, 5, 1, 6, 1, 7, 7, 42, 42, 42, 42, 42] | |
| const findMostRecurrent = param => { | |
| return param.reduce((acc, curr, index, base) => { | |
| const currSum = base.filter( it => it === curr).length | |
| const newAcc = { ...acc, [curr]: currSum } | |
| if (++index == base.length) { | |
| const arrObj = Object.keys(newAcc) | |
| .map(it => ({ [it]: acc[it] })) | |
| .sort((a, b) => Object.values(a)[0] < Object.values(b)[0]) | |
| return +Object.keys(arrObj[0]) | |
| } | |
| return newAcc | |
| }) | |
| } | |
| const mostRecurrentNum = findMostRecurrent(arr) | |
| console.log("mostRecurrentNum ===>", mostRecurrentNum) | |
| //TODO: cover when two numbers apears same number of times on most recurrency |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment