Skip to content Skip to sidebar Skip to footer

Removing Duplicate Element In An Array

Possible Duplicate: Easiest way to find duplicate values in a JavaScript array Javascript array sort and unique I have the following array var output = new array(7); output[0

Solution 1:

You can write a function like this

function eliminateDuplicates(arr) {
var i,
  len=arr.length,
  out=[],
  obj={};

 for (i=0;i<len;i++) {
 obj[arr[i]]=0;
 }
 for (i in obj) {
 out.push(i);
 }
 returnout;
}`

Check this here

Solution 2:

Maybe more complex than you need but:

function array_unique (inputArr) {
    // Removes duplicate values from array  var key = '',
        tmp_arr2 = {},
        val = '';

    var __array_search = function (needle, haystack) {
        var fkey = '';
        for (fkey in haystack) {
            if (haystack.hasOwnProperty(fkey)) {
                if ((haystack[fkey] + '') === (needle + '')) {
                    return fkey;
                }
            }
        }
        returnfalse;
    };

    for (key in inputArr) {
        if (inputArr.hasOwnProperty(key)) {
            val = inputArr[key];
            if (false === __array_search(val, tmp_arr2)) {
                tmp_arr2[key] = val;
            }
        }
    }

    return tmp_arr2;
}

Code taken from: http://phpjs.org/functions/array_unique:346

Solution 3:

You can remove dups from an array by using a temporary hash table (using a javascript object) to keep track of which images you've already seen in the array. This works for array values that can be uniquely represented as a string (strings or numbers mostly), but not for objects.

functionremoveDups(array) {
    var index = {};
    // traverse array from end to start // so removing the current item from the array// doesn't mess up the traversalfor (var i = array.length - 1; i >= 0; i--) {
        if (array[i] in index) {
            // remove this itemarray.splice(i, 1);
        } else {
            // add this value to index
            index[array[i]] = true;
        }
    }
}

Here's a working example: http://jsfiddle.net/jfriend00/sVT7g/

For sizable arrays, using an object as a temporary index will be many times faster than a linear search of the array.

Solution 4:

First of all, you'll want to use the array literal (var output = []) to declare your array. Second, you'll want to loop through your array and store all the values in a second array. If any value in the first array matches a value in the second array, delete it and continue looping.

Your code would look like this:

var output = [
    "Rose",
    "India",
    "Technologies",
    "Rose",
    "Ltd",
    "India",
    "Rose"
]

var doubledOutput = [];

for(var i = 0; i < output.length; i++) {
    var valueIsInArray = false;

    for(var j = 0; j < doubledOutput.length; j++) {
        if(doubledOutput[j] == output[i]) {
            valueIsInArray = true;
        }
    }

    if(valueIsInArray) {
        output.splice(i--, 1);
    } else {
        doubledOutput.push(output[i]);
    }
}

Please note, the above code is untested and may contain errors.

Post a Comment for "Removing Duplicate Element In An Array"