Skip to content

Instantly share code, notes, and snippets.

@creotip
Last active August 31, 2022 07:07
Show Gist options
  • Save creotip/f2b4dcea0c04d85853e96e0bd6e1c999 to your computer and use it in GitHub Desktop.
Save creotip/f2b4dcea0c04d85853e96e0bd6e1c999 to your computer and use it in GitHub Desktop.
Javascript: Find Peak timestamp in array of min-max timestamp values
function findPeak(arr) {
const peaks = []
let num = 0
let occur = 0
arr.forEach(item => {
for (let i = item.min; i < item.max; i++) {
peaks.push(i)
}
})
let hashMap = peaks.reduce((acc, value) => {
acc[value] = acc[value] ? acc[value] + 1 : 1
return acc
}, {})
Object.keys(hashMap).forEach(item => {
if (hashMap[item] > occur) {
occur = hashMap[item]
num = item
}
})
return +num
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment