Skip to content

Instantly share code, notes, and snippets.

@JCMais

JCMais/gist:1606497

Created Jan 13, 2012
Embed
What would you like to do?
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
You can’t perform that action at this time.