# 获取数组中的所有非唯一值（即：重复/多次出现）

2020/09/27 17:21 · javascript ·  · 0评论

### 类似的问题：

``````const findDuplicates = (arr) => {
let sorted_arr = arr.slice().sort(); // You can define the comparing function here.
// JS by default uses a crappy string compare.
// (we use slice to clone the array so the
// original array won't be modified)
let results = [];
for (let i = 0; i < sorted_arr.length - 1; i++) {
if (sorted_arr[i + 1] == sorted_arr[i]) {
results.push(sorted_arr[i]);
}
}
return results;
}

let duplicatedArray = [9, 9, 111, 2, 3, 4, 4, 5, 7];
console.log(`The duplicates in \${duplicatedArray} are \${findDuplicates(duplicatedArray)}`);``````

``````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;
}
``````

## 查找重复项

``````var names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl']

var uniq = names
.map((name) => {
return {
count: 1,
name: name
}
})
.reduce((a, b) => {
a[b.name] = (a[b.name] || 0) + b.count
return a
}, {})

var duplicates = Object.keys(uniq).filter((a) => uniq[a] > 1)

console.log(duplicates) // [ 'Nancy' ]``````

## 更多功能语法：

@ Dmytro-Laptin指出了一些要删除的代码。这是相同代码的更紧凑版本。使用一些ES6技巧和高阶函数：

``````const names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl']

const count = names =>
names.reduce((a, b) => ({ ...a,
[b]: (a[b] || 0) + 1
}), {}) // don't forget to initialize the accumulator

const duplicates = dict =>
Object.keys(dict).filter((a) => dict[a] > 1)

console.log(count(names)) // { Mike: 1, Matt: 1, Nancy: 2, Adam: 1, Jenny: 1, Carl: 1 }
console.log(duplicates(count(names))) // [ 'Nancy' ]``````

## 在数组中查找重复值

``````var input = [1, 2, 3, 1, 3, 1];

var duplicates = input.reduce(function(acc, el, i, arr) {
if (arr.indexOf(el) !== i && acc.indexOf(el) < 0) acc.push(el); return acc;
}, []);

document.write(duplicates); // = 1,3 (actual array == [1, 3])``````

``````[1, 2, 2, 4, 3, 4].filter((e, i, a) => a.indexOf(e) !== i) // [2, 4]
``````

``````[1, 2, 2, 4, 3, 4].filter((e, i, a) => a.indexOf(e) === i) // [1, 2, 3, 4]
``````

``````[1, 2, 3].every((e, i, a) => a.indexOf(e) === i) // true

[1, 2, 1].every((e, i, a) => a.indexOf(e) === i) // false
``````

``````Array.prototype.unique = function () {
var r = new Array();
o:for(var i = 0, n = this.length; i < n; i++)
{
for(var x = 0, y = r.length; x < y; x++)
{
if(r[x]==this[i])
{
continue o;
}
}
r[r.length] = this[i];
}
return r;
}

var arr = [1,2,2,3,3,4,5,6,2,3,7,8,5,9];
var unique = arr.unique();
``````

``````var unique = function(){
var hasOwn = {}.hasOwnProperty,
toString = {}.toString,
uids = {};

function uid(){
var key = Math.random().toString(36).slice(2);
return key in uids ? uid() : uids[key] = key;
}

function unique(array){
var strings = {}, numbers = {}, others = {},
tagged = [], failed = [],
count = 0, i = array.length,
item, type;

var id = uid();

while (i--) {
item = array[i];
type = typeof item;
if (item == null || type !== 'object' && type !== 'function') {
// primitive
switch (type) {
case 'string': strings[item] = true; break;
case 'number': numbers[item] = true; break;
default: others[item] = item; break;
}
} else {
// object
if (!hasOwn.call(item, id)) {
try {
item[id] = true;
tagged[count++] = item;
} catch (e){
if (failed.indexOf(item) === -1)
failed[failed.length] = item;
}
}
}
}

// remove the tags
while (count--)
delete tagged[count][id];

tagged = tagged.concat(failed);
count = tagged.length;

// append primitives to results
for (i in strings)
if (hasOwn.call(strings, i))
tagged[count++] = i;

for (i in numbers)
if (hasOwn.call(numbers, i))
tagged[count++] = +i;

for (i in others)
if (hasOwn.call(others, i))
tagged[count++] = others[i];

return tagged;
}

return unique;
}();
``````

``````function unique(array){
var seen = new Set;
return array.filter(function(item){
if (!seen.has(item)) {
return true;
}
});
}
``````
``````var a = ["a","a","b","c","c"];

a.filter(function(value,index,self){ return (self.indexOf(value) !== index )})
``````

``````function find_duplicates(arr) {
var len=arr.length,
out=[],
counts={};

for (var i=0;i<len;i++) {
var item = arr[i];
counts[item] = counts[item] >= 1 ? counts[item] + 1 : 1;
if (counts[item] === 2) {
out.push(item);
}
}

return out;
}

find_duplicates(['one',2,3,4,4,4,5,6,7,7,7,'pig','one']); // -> ['one',4,7] in no particular order.
``````

``````function hasDuplicate(arr){
return (arr.length != _.uniq(arr).length);
}
``````

## ES2015

``````//          🚩🚩   🚩                 🚩
var arr =  [1,2,2,3,3,4,5,6,2,3,7,8,5,22],
arr2 = [1,2,511,12,50],
arr3 = [22],
unique;

// Combine all the arrays to a single one
unique = arr.concat(arr2, arr3)

// create a new (dirty) Array with only the unique items
unique = unique.map((item,i) => unique.includes(item, i+1) ? item : '' )

// Cleanup - remove duplicate & empty items items
unique = [...new Set(unique)].filter(n => n)

console.log(unique)``````

# 从3个数组中查找唯一值（或更多）：

``````Array.prototype.unique = function () {
var arr = this.sort(), i; // input must be sorted for this to work
for( i=arr.length; i--; )
arr[i] === arr[i-1] && arr.splice(i,1); // remove duplicate item

return arr;
}

var arr =  [1,2,2,3,3,4,5,6,2,3,7,8,5,9],
arr2 = [1,2,511,12,50],
arr3 = [22],
// merge arrays & call custom Array Prototype - "unique"
unique = arr.concat(arr2, arr3).unique();

console.log(unique);  // [22, 50, 12, 511, 2, 1, 9, 5, 8, 7, 3, 6, 4]
``````

### 只是旧浏览器的数组indexOf的polyfill：

``````if (!Array.prototype.indexOf){
Array.prototype.indexOf = function(elt /*, from*/){
var len = this.length >>> 0;

var from = Number(arguments[1]) || 0;
from = (from < 0) ? Math.ceil(from) : Math.floor(from);
if (from < 0)
from += len;

for (; from < len; from++){
if (from in this && this[from] === elt)
return from;
}
return -1;
};
}
``````

## jQuery解决方案使用“ inArray”：

``````if( \$.inArray(this[i], arr) == -1 )
``````

``````var array = [1, 2, 2, 3, 3, 4, 5, 6, 2, 3, 7, 8, 5, 22, 1, 2, 511, 12, 50, 22];

console.log([...new Set(
array.filter((value, index, self) => self.indexOf(value) !== index))]
);``````

``````let a = [1, 2, 3, 4, 2, 2, 4, 1, 5, 6]
let b = [...new Set(a.sort().filter((o, i) => o !== undefined && a[i + 1] !== undefined && o === a[i + 1]))]

// b is now [1, 2, 4]
``````
``````var a = [324,3,32,5,52,2100,1,20,2,3,3,2,2,2,1,1,1].sort();
a.filter(function(v,i,o){return i&&v!==o[i-1]?v:0;});
``````

``````//copy and paste: without error handling
Array.prototype.unique =
function(){return this.sort().filter(function(v,i,o){return i&&v!==o[i-1]?v:0;});}
``````

``````const arr = ['hi', 'hi', 'hi', 'bye', 'bye', 'asd']
const {
dup
} = arr.reduce(
(acc, curr) => {
acc.items[curr] = acc.items[curr] ? acc.items[curr] += 1 : 1
if (acc.items[curr] === 2) acc.dup.push(curr)
return acc
}, {
items: {},
dup: []
},
)

console.log(dup)
// ['hi', 'bye']``````

``````    const arr = [-1, 2, 2, 2, 0, 0, 0, 500, -1, 'a', 'a', 'a']

const filtered = arr.filter((el, index) => arr.indexOf(el) !== index)
// => filtered = [ 2, 2, 0, 0, -1, 'a', 'a' ]

const duplicates = [...new Set(filtered)]

console.log(duplicates)
// => [ 2, 0, -1, 'a' ]
``````

1. 它适用于任何数字，包括`0`，字符串和负数，例如`-1`-
相关问题： 获取JavaScript数组中的所有唯一值（删除重复项）

2. `arr`保留原始数组`filter`返回新数组而不是修改原始数组）

3. `filtered`数组包含所有重复项。可以包含1个以上相同的值（例如，这里的过滤数组是`[ 2, 2, 0, 0, -1, 'a', 'a' ]`

4. 如果你想获得是重复的值（你不希望有相同值的多个副本），可以使用`[...new Set(filtered)]`（ES6都有一个对象可存储唯一的值）

``````[1,1,2,2,2,3].filter((v,i,a) => a.indexOf(v) !== i) // [1, 2, 2]
``````

``````var arr = [9,1,2,4,3,4,9]
console.log(arr.filter((ele,indx)=>indx!==arr.indexOf(ele))) //get the duplicates
console.log(arr.filter((ele,indx)=>indx===arr.indexOf(ele))) //remove the duplicates``````

``````var codes = dc_1.split(',');
var i = codes.length;
while (i--) {
if (codes.indexOf(codes[i]) != i) {
codes.splice(i,1);
}
}
``````

``````var duplicates = myArray.filter(i => myArray.filter(ii => ii === i).length > 1);
``````

https://es6console.com/j58euhbt/

``````let duplicates = a => {d=[]; a.sort((a,b) => a-b).reduce((a,b)=>{a==b&&!d.includes(a)&&d.push(a); return b}); return d};
``````

``````duplicates([1,2,3,10,10,2,3,3,10]);
``````

``````const result = [1, 2, 2, 3, 3, 3, 3].reduce((x, y) => x.includes(y) ? x : [...x, y], []);

console.log(result);``````

``````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;
}
``````

``````var arrayToFilter = [ 4, 5, 5, 5, 2, 1, 3, 1, 1, 2, 1, 3 ];

arrayToFilter.
sort().
filter( function(me,i,arr){
return (i===0) || ( me !== arr[i-1] );
});
``````
``````var arr = [2, 1, 2, 2, 4, 4, 2, 5];

function returnDuplicates(arr) {
return arr.reduce(function(dupes, val, i) {
if (arr.indexOf(val) !== i && dupes.indexOf(val) === -1) {
dupes.push(val);
}
return dupes;
}, []);
}

``````function toUnique(a,b,c){//array,placeholder,placeholder
b=a.length;
while(c=--b)while(c--)a[b]!==a[c]||a.splice(c,1)
}
var array=[1,2,3,4,5,6,7,8,9,0,1,2,1];
toUnique(array);
console.log(array);
``````

`toUnique`删除重复项的重复项。

``````function theDuplicates(a,b,c,d){//array,placeholder,placeholder
b=a.length,d=[];
while(c=--b)while(c--)a[b]!==a[c]||d.push(a.splice(c,1))
}
var array=[1,2,3,4,5,6,7,8,9,0,1,2,1];

toUnique(theDuplicates(array));
``````

``````var arr = [1, 1, 4, 5, 5], darr = [], duplicates = [];

for(var i = 0; i < arr.length; i++){
if(darr.includes(arr[i]) && !duplicates.includes(arr[i]))
duplicates.push(arr[i])
else
darr.push(arr[i]);
}

console.log(duplicates);``````
``````<h3>Array with duplicates</h3>
<p>[1, 1, 4, 5, 5]</p>
<h3>Array with distinct elements</h3>
<p>[1, 4, 5]</p>
<h3>duplicate values are</h3>
<p>[1, 5]</p>``````

ES6提供了Set数据结构，该结构基本上是一个不接受重复项的数组。使用Set数据结构，有一种非常简单的方法来查找数组中的重复项（仅使用一个循环）。

``````function findDuplicate(arr) {
var set = new Set();
var duplicates = new Set();
for (let i = 0; i< arr.length; i++) {
var size = set.size;
if (set.size === size) {
}
}
return duplicates;
}
``````

``````    var list = [9, 9, 111, 2, 3, 4, 4, 5, 7];

// Filter 1: to find all duplicates elements
var duplicates = list.filter(function(value,index,self) {
return self.indexOf(value) !== self.lastIndexOf(value) && self.indexOf(value) === index;
});

console.log(duplicates);``````

``````// @Param:data:Array that is the source
// @Return : Array that have the duplicate entries
findDuplicates(data: Array<any>): Array<any> {
return Array.from(new Set(data)).filter((value) => data.indexOf(value) !== data.lastIndexOf(value));
}
``````

1. 单行：-P
2. 所有内置的数据结构有助于提高效率
3. 快点

1. 转换为设置以删除所有重复项
2. 遍历设置值
3. 对于源数组中的每个设置值，检查条件“值第一个索引不等于最后一个索引” ==>然后推断为重复，否则为“唯一”