Skip to content

Instantly share code, notes, and snippets.

@wjuniorw
Created August 14, 2022 07:01
Show Gist options
  • Select an option

  • Save wjuniorw/34a8afbde3d92dfdc52055508b94e576 to your computer and use it in GitHub Desktop.

Select an option

Save wjuniorw/34a8afbde3d92dfdc52055508b94e576 to your computer and use it in GitHub Desktop.
find most recurrent number...
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