Created
May 11, 2019 15:53
-
-
Save violet-athena/dedfeb55ebb4ce9ccda1c245535c9fdd to your computer and use it in GitHub Desktop.
Find peaks including plateaus in a 1D array
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 findPeaks = arr => arr.reduce((result, curr, i) => { | |
const prev = arr[i - 1]; | |
const next = arr.slice(i).find(item => item !== curr); | |
if(curr > prev && curr > next) { | |
result.pos.push(i); | |
result.peaks.push(curr); | |
} | |
return result; | |
}, { | |
pos:[], | |
peaks:[], | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment