Skip to content

Instantly share code, notes, and snippets.

@violet-athena
Created May 11, 2019 15:53
Show Gist options
  • Save violet-athena/dedfeb55ebb4ce9ccda1c245535c9fdd to your computer and use it in GitHub Desktop.
Save violet-athena/dedfeb55ebb4ce9ccda1c245535c9fdd to your computer and use it in GitHub Desktop.
Find peaks including plateaus in a 1D array
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