Created
January 13, 2012 14:31
-
-
Save JCMais/1606497 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Array.prototype.duplicates = function ( ) { | |
return this.filter ( function ( x , y , k ) { | |
return y !== k.lastIndexOf ( x ); | |
}); | |
}; | |
Array.prototype.hasDuplicates = function ( ) { | |
var me = this; | |
return me.some ( function ( idx ) { | |
return me.indexOf( idx ) !== me.lastIndexOf ( idx ); | |
}); | |
}; | |
var arr = [ 'a' , 'b' , 'c' , 'd' , 'a' , 'b' , 'c' , 'e' ]; | |
console.group( 'Array Duplicada' ); | |
console.log( arr.hasDuplicates() ); | |
if( arr.hasDuplicates() ){ | |
console.log( arr.duplicates() ); | |
} | |
console.groupEnd( 'Array Duplicada' ); | |
console.group( 'Array Normal' ); | |
var arr2 = [ 'a' , 'b' , 'c' , 'e' ]; | |
console.log( arr2.hasDuplicates() ); | |
if( arr2.hasDuplicates() ){ | |
console.log( arr2.duplicates() ); | |
} | |
console.groupEnd( 'Array Normal' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment