Created
January 19, 2015 07:27
-
-
Save sdtsui/4b59504156713223132a to your computer and use it in GitHub Desktop.
S18 - hasDuplicates Time Complexity
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
var hasDuplicates = function(array){ | |
for(var i = 0; i < array.length; i++){ | |
var item = array[i]; | |
if(array.slice(i+1).indexOf(item) !== -1){ | |
return true; | |
} | |
} | |
return false; | |
}; | |
/* | |
Line 2: Loop time complexity linear. | |
Line 4 worst case: | |
n-i operations for the slice, and n-i operations for the indexOf | |
So 2n-2i, where i is a constant less than N. | |
n*(Cn - Y) reduces to O(n^2). | |
Credit to John Tan for explaining this to me. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment