Skip to content

Instantly share code, notes, and snippets.

@sdtsui
Created January 19, 2015 07:27
Show Gist options
  • Save sdtsui/4b59504156713223132a to your computer and use it in GitHub Desktop.
Save sdtsui/4b59504156713223132a to your computer and use it in GitHub Desktop.
S18 - hasDuplicates Time Complexity
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