Get all Indexes of Duplicate Entries in an array

Array.prototype.collectDuplicateIndices = function (value)
// Returns true if the passed value is found in the
// array. Returns false if it is not.

{
var i;
var dupesStr = “”;
for (i=0; i < this.length; i++) { // use === to check for Matches. ie., identical (===), ; if (this[i] == value) { dupesStr += i+","; } } return (dupesStr>“”)?dupesStr:”null”;
};

This will return all the index/positions of the duplicate entries in an Array as a string seperated with commas.

For example the Returned string will be 1,77,55
Where 1,77 and 55 are the positions of the duplicate entries in the array. ( Or you can say those positions are having the duplicate entries)

Leave a Reply

Your email address will not be published. Required fields are marked *