Skip to content

Instantly share code, notes, and snippets.

@shockey
Last active August 29, 2015 14:26
Show Gist options
  • Save shockey/58d03f8e3ea49c9f32d0 to your computer and use it in GitHub Desktop.
Save shockey/58d03f8e3ea49c9f32d0 to your computer and use it in GitHub Desktop.
linear-time min/max helper functions for arrays of numbers
// time: O(n)
// space: O(1)
var max = function(arr) {
var returnable = Number.MIN_VALUE;
arr.forEach(function(v){
if(returnable < v) {
returnable = v;
}
})
return returnable;
}
var min = function(arr) {
var returnable = Number.MAX_VALUE;
arr.forEach(function(v){
if(returnable > v) {
returnable = v;
}
})
return returnable;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment