/*
The indexOf method of the Array object is useful for comparing array items.
IE is the only major browser that does not natively support it, but it is easy to implement:
*/
Array.prototype.indexOf= Array.prototype.indexOf || function(what, i){
i= i || 0;
var L= this.length;
while(i<L){
if(this[i]=== what) return i;
++i;
}
return -1;
}
function getarrayduplicates(arg){
var itm, A= arg.slice(0, arg.length), dups= [];
while(A.length){
itm= A.shift();
if(A.indexOf(itm)!= -1 && dups.indexOf(itm)== -1){
dups[dups.length]= itm;
}
}
return dups;
}
var a1= [1, 22, 3, 2, 2, 3, 3, 4, 1, 22, 7, 8, 9];
alert(getarrayduplicates(a1));
For very large arrays, it can be faster to remove the duplicates from the array as they are found, so that they will not be looked at again:
function getarrayduplicates(arg){
var itm, A= arg.slice(0, arg.length), dups= [];
while(A.length){
itm= A.shift();
if(A.indexOf(itm)!= -1){
dups[dups.length]= itm;
while(A.indexOf(itm)!= -1){
A.splice(A.indexOf(itm), 1);
}
}
}
return dups;
}