Skip to content

Instantly share code, notes, and snippets.

@JCMais
Created January 13, 2012 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JCMais/1606497 to your computer and use it in GitHub Desktop.
Save JCMais/1606497 to your computer and use it in GitHub Desktop.
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