我需要检查一个JavaScript数组,看看是否有任何重复的值。最简单的方法是什么?我只需要找到复制的值是什么——实际上我不需要它们的索引,也不需要它们复制了多少次。
我知道我可以循环遍历数组并检查所有其他值是否匹配,但似乎应该有更简单的方法。
类似的问题:
获取JavaScript数组中所有唯一的值(删除重复值)
我需要检查一个JavaScript数组,看看是否有任何重复的值。最简单的方法是什么?我只需要找到复制的值是什么——实际上我不需要它们的索引,也不需要它们复制了多少次。
我知道我可以循环遍历数组并检查所有其他值是否匹配,但似乎应该有更简单的方法。
类似的问题:
获取JavaScript数组中所有唯一的值(删除重复值)
当前回答
这是我用map实现的。它应该运行在O(n)时间,应该有点容易喘气。
var first_array = [1, 1, 2, 3, 4, 4, 5, 6); var find_dup=new Map; For (first_array的const迭代器){ //如果现值++ 如果(find_dup.has (iterator)) { find_dup.set(迭代器,find_dup.get(迭代器)+ 1); 其他}{ //否则添加 find_dup.set(迭代器,1); } } console.log (find_dup.get (2));
然后你可以find_dup.get(key)来查找它是否有重复(它应该给> 1)。
其他回答
这个答案可能也有帮助,它利用js的reduce操作符/方法从数组中删除重复项。
Const result =[1,2,2,3,3,3,3]。Reduce ((x, y) => x.includes(y) ?X:[…]X, y], []); console.log(结果);
http://jsfiddle.net/vol7ron/gfJ28/
var arr = ['hello','goodbye','foo','hello','foo','bar',1,2,3,4,5,6,7,8,9,0,1,2,3];
var hash = [];
// build hash
for (var n=arr.length; n--; ){
if (typeof hash[arr[n]] === 'undefined') hash[arr[n]] = [];
hash[arr[n]].push(n);
}
// work with compiled hash (not necessary)
var duplicates = [];
for (var key in hash){
if (hash.hasOwnProperty(key) && hash[key].length > 1){
duplicates.push(key);
}
}
alert(duplicates);
The result will be the hash array, which will contain both a unique set of values and the position of those values. So if there are 2 or more positions, we can determine that the value has a duplicate. Thus, every place hash[<value>].length > 1, signifies a duplicate. hash['hello'] will return [0,3] because 'hello' was found in node 0 and 3 in arr[]. Note: the length of [0,3] is what's used to determine if it was a duplicate. Using for(var key in hash){ if (hash.hasOwnProperty(key)){ alert(key); } } will alert each unique value.
下面的函数(前面提到的eliminateduplates函数的变体)似乎可以做到这一点,它为输入["test", "test2", "test2", 1,1,2,3,4,5,6,7,7,10,22,43,1,5,5,8]返回test2,1,7,7,8]。
请注意,这个问题在JavaScript中比在大多数其他语言中更奇怪,因为JavaScript数组几乎可以容纳任何东西。注意,使用排序的解决方案可能需要提供适当的排序函数——我还没有尝试过这种方法。
这个特殊的实现(至少)适用于字符串和数字。
function findDuplicates(arr) {
var i,
len=arr.length,
out=[],
obj={};
for (i=0;i<len;i++) {
if (obj[arr[i]] != null) {
if (!obj[arr[i]]) {
out.push(arr[i]);
obj[arr[i]] = 1;
}
} else {
obj[arr[i]] = 0;
}
}
return out;
}
如果你想消除重复,试试这个好方法:
函数eliminateduplicate (arr) { var我, Len = arrr .length, Out = [], Obj = {}; For (i = 0;I < len;我+ +){ Obj [arr[i]] = 0; } 对于(i in obj) { out.push(我); } 返回; } console.log (eliminateDuplicates([1、6、7、3、6、8、1,3,4,5,1、7、2、6]))
来源: http://dreaminginjavascript.wordpress.com/2008/08/22/eliminating-duplicates/
下面是一个没有使用临时数组来存储非重复的数组:
// simple duplicate removal for non-object types
Array.prototype.removeSimpleDupes = function() {
var i, cntr = 0, arr = this, len = arr.length;
var uniqueVal = function(val,n,len) { // remove duplicates
var dupe = false;
for (i = n; i < len; i++) {
if (typeof arr[i]!=="undefined" && val===arr[i]) { arr.splice(i,1); dupe = true; }
}
return (dupe) ? arr.length : len;
};
while (cntr < len) {
len = uniqueVal(arr[cntr],cntr+1,len);
cntr++;
}
return arr;
};