Removing duplicate element in an array [duplicate]

泪湿孤枕 提交于 2019-12-12 16:28:55

问题


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]="Rose";
  output[1]="India";
  output[2]="Technologies";
  output[3]="Rose";
  output[4]="Ltd";
  output[5]="India";
  output[6]="Rose";

how can i remove the duplicate elements in above array.Is there any methods to do it?


回答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);
 }
 return out;
}`

Check this here




回答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;
                }
            }
        }
        return false;
    };

    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




回答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.

function removeDups(array) {
    var index = {};
    // traverse array from end to start 
    // so removing the current item from the array
    // doesn't mess up the traversal
    for (var i = array.length - 1; i >= 0; i--) {
        if (array[i] in index) {
            // remove this item
            array.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.




回答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.



来源:https://stackoverflow.com/questions/9751413/removing-duplicate-element-in-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!