Skip to content

Instantly share code, notes, and snippets.

@galenweber
Created April 22, 2016 18:10
Show Gist options
  • Save galenweber/0cc8e7d67cfb6a69b51b001ecb76abc1 to your computer and use it in GitHub Desktop.
Save galenweber/0cc8e7d67cfb6a69b51b001ecb76abc1 to your computer and use it in GitHub Desktop.
// Given an array of integers, find a peak if it exists
// Where a peak is defined as greater than or equal to previous
// and subsequent values (for edge, >= to sole neighbor)
var findPeak = function(array) {
for (var i=1; i<array.length; i++) {
if ((array[i] >= array[i-1]) && (array[i] >= array[i+1])) {
return i;
}
}
// No peak was found outside the edges, return the higher edge
return (array[0] > array[array.length-1]) ? 0 : array.length -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment