Created
April 22, 2016 18:10
-
-
Save galenweber/0cc8e7d67cfb6a69b51b001ecb76abc1 to your computer and use it in GitHub Desktop.
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
// 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