Archive for January, 2006

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)

Check if an element or value exists in an array or not

This function will Check if an element or value exists in an array or not.
And return true if the passed value is found in the array. Returns false if it is not found in the array.

Array.prototype.inArray = function (value,caseSensitive)
// Returns true if the passed value is found in the
// array. Returns false if it is not.
{
var i;
for (i=0; i < this.length; i++) { // use === to check for Matches. ie., identical (===), if(caseSensitive){ //performs match even the string is case sensitive if (this[i].toLowerCase() == value.toLowerCase()) { return true; } }else{ if (this[i] == value) { return true; } } } return false; }; Note : If you don't want to perform case sensitive check, then set caseSensitve=true and call the function.

Feedback Form