Last active
August 29, 2015 14:26
-
-
Save shockey/58d03f8e3ea49c9f32d0 to your computer and use it in GitHub Desktop.
linear-time min/max helper functions for arrays of numbers
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
// 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