Last active
August 31, 2022 07:07
-
-
Save creotip/f2b4dcea0c04d85853e96e0bd6e1c999 to your computer and use it in GitHub Desktop.
Javascript: Find Peak timestamp in array of min-max timestamp values
This file contains 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
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