Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
JavascriptArraysJavascript Problem Overview
I need to check a JavaScript array to see if there are any duplicate values. What's the easiest way to do this? I just need to find what the duplicated values are - I don't actually need their indexes or how many times they are duplicated.
I know I can loop through the array and check all the other values for a match, but it seems like there should be an easier way.
Similar question:
Javascript Solutions
Solution 1 - Javascript
You could sort the array and then run through it and then see if the next (or previous) index is the same as the current. Assuming your sort algorithm is good, this should be less than O(n2):
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)}`);
In case, if you are to return as a function for duplicates. This is for similar type of case.
Reference: https://stackoverflow.com/a/57532964/8119511
Solution 2 - Javascript
If you want to elimate the duplicates, try this great solution:
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;
}
console.log(eliminateDuplicates([1,6,7,3,6,8,1,3,4,5,1,7,2,6]))
Source: http://dreaminginjavascript.wordpress.com/2008/08/22/eliminating-duplicates/
Solution 3 - Javascript
This is my answer from the duplicate thread (!):
When writing this entry 2014 - all examples were for
-loops or jQuery. JavaScript has the perfect tools for this: sort
, map
and reduce
.
Find duplicate items
var names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl']
const uniq = names
.map((name) => {
return {
count: 1,
name: name
};
})
.reduce((result, b) => {
result[b.name] = (result[b.name] || 0) + b.count;
return result;
}, {});
const duplicates = Object.keys(uniq).filter((a) => uniq[a] > 1);
console.log(duplicates); // [ 'Nancy' ]
More functional syntax:
@Dmytro-Laptin pointed out some code that can be removed. This is a more compact version of the same code. Using some ES6 tricks and higher-order functions:
const names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl'];
const count = names =>
names.reduce((result, value) => ({ ...result,
[value]: (result[value] || 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' ]
Solution 4 - Javascript
UPDATED: Short one-liner to get the duplicates:
[1, 2, 2, 4, 3, 4].filter((e, i, a) => a.indexOf(e) !== i) // [2, 4]
To get the array without duplicates simply invert the condition:
[1, 2, 2, 4, 3, 4].filter((e, i, a) => a.indexOf(e) === i) // [1, 2, 3, 4]
I simply did not think about filter()
in my old answer below ;)
When all you need is to check that there are no duplicates as asked in this question you can use the every()
method:
[1, 2, 3].every((e, i, a) => a.indexOf(e) === i) // true
[1, 2, 1].every((e, i, a) => a.indexOf(e) === i) // false
Note that every()
doesn't work for IE 8 and below.
Solution 5 - Javascript
Find duplicate values in an array
This should be one of the shortest ways to actually find duplicate values in an array. As specifically asked for by the OP, this does not remove duplicates but finds them.
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])
This doesn't need sorting or any third party framework. It also doesn't need manual loops. It works with every value indexOf() (or to be clearer: the strict comparision operator) supports.
Solution 6 - Javascript
You can add this function, or tweak it and add it to Javascript's Array prototype:
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])
{
alert('this is a DUPE!');
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();
alert(unique);
Solution 7 - Javascript
UPDATED: The following uses an optimized combined strategy. It optimizes primitive lookups to benefit from hash O(1) lookup time (running unique
on an array of primitives is O(n)). Object lookups are optimized by tagging objects with a unique id while iterating through so so identifying duplicate objects is also O(1) per item and O(n) for the whole list. The only exception is items that are frozen, but those are rare and a fallback is provided using an array and indexOf.
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;
}();
If you have ES6 Collections available, then there is a much simpler and significantly faster version. (shim for IE9+ and other browsers here: https://github.com/Benvie/ES6-Harmony-Collections-Shim)
function unique(array){
var seen = new Set;
return array.filter(function(item){
if (!seen.has(item)) {
seen.add(item);
return true;
}
});
}
Solution 8 - Javascript
var a = ["a","a","b","c","c"];
a.filter(function(value,index,self){ return (self.indexOf(value) !== index )})
Solution 9 - Javascript
This should get you what you want, Just the duplicates.
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.
Solution 10 - Javascript
Find non-unique values from 3 arrays (or more):
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,0],
merged,
nonUnique;
// Combine all the arrays to a single one
merged = arr.concat(arr2, arr3)
// create a new (dirty) Array with only the non-unique items
nonUnique = merged.filter((item,i) => merged.includes(item, i+1))
// Cleanup - remove duplicate & empty items items
nonUnique = [...new Set(nonUnique)]
console.log(nonUnique)
PRE-ES2015:
In the below example I chose to superimpose a unique
method on top of the Array
prototype, allowing access from everywhere and has more "declarative" syntax. I do not recommend this approach on large projects, since it might very well collide with another method with the same custom name.
Array.prototype.unique = function () {
var arr = this.sort(), i=arr.length; // input must be sorted for this to work
while(i--)
arr[i] === arr[i-1] && arr.splice(i,1) // remove duplicate item
return arr
}
Array.prototype.nonunique = function () {
var arr = this.sort(), i=arr.length, res = []; // input must be sorted for this to work
while(i--)
arr[i] === arr[i-1] && (res.indexOf(arr[i]) == -1) && res.push(arr[i])
return res
}
// 🚩🚩 🚩 🚩 🚩
var arr = [1,2,2,3,3,4,5,6,2,3,7,8,5,22],
arr2 = [1,2,511,12,50],
arr3 = [22,0],
// merge all arrays & call custom Array Prototype - "unique"
unique = arr.concat(arr2, arr3).unique(),
nonunique = arr.concat(arr2, arr3).nonunique()
console.log(unique) // [1,12,2,22,3,4,5,50,511,6,7,8]
console.log(nonunique) // [1,12,2,22,3,4,5,50,511,6,7,8]
Solution 11 - Javascript
using underscore.js
function hasDuplicate(arr){
return (arr.length != _.uniq(arr).length);
}
Solution 12 - Javascript
Here's the simplest solution I could think of:
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' ]
That's it.
Note:
-
It works with any numbers including
0
, strings and negative numbers e.g.-1
- Related question: https://stackoverflow.com/questions/1960473 -
The original array
arr
is preserved (filter
returns the new array instead of modifying the original) -
The
filtered
array contains all duplicates; it can also contain more than 1 same value (e.g. our filtered array here is[ 2, 2, 0, 0, -1, 'a', 'a' ]
) -
If you want to get only values that are duplicated (you don't want to have multiple duplicates with the same value) you can use
[...new Set(filtered)]
(ES6 has an object Set which can store only unique values)
Hope this helps.
Solution 13 - Javascript
The simplest and quickest way is to use the Set
object:
const numbers = [1, 2, 3, 2, 4, 5, 5, 6];
const set = new Set(numbers);
const duplicates = numbers.filter(item => {
if (set.has(item)) {
set.delete(item);
} else {
return item;
}
});
console.log(duplicates);
// [ 2, 5 ]
Solution 14 - Javascript
Here is mine simple and one line solution.
It searches not unique elements first, then makes found array unique with the use of Set.
So we have array of duplicates in the end.
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))]
);
Solution 15 - Javascript
This is my proposal (ES6):
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]
Solution 16 - Javascript
one liner simple way
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
Solution 17 - Javascript
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;});
or when added to the prototyp.chain of Array
//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;});}
See here: https://gist.github.com/1305056
Solution 18 - Javascript
Fast and elegant way using es6 object destructuring and reduce
It runs in O(n) (1 iteration over the array) and doesn't repeat values that appear more than 2 times
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']
Solution 19 - Javascript
Shortest vanilla JS:
[1,1,2,2,2,3].filter((v,i,a) => a.indexOf(v) !== i) // [1, 2, 2]
Solution 20 - Javascript
You can use filter method and indexOf() to get all the duplicate values
function duplicate(arr) {
return duplicateArray = arr.filter((item, index) => arr.indexOf(item) !== index)
}
arr.indexOf(item) will always return the first index at which a given element can be found
Solution 21 - Javascript
Here is a very light and easy way:
var codes = dc_1.split(',');
var i = codes.length;
while (i--) {
if (codes.indexOf(codes[i]) != i) {
codes.splice(i,1);
}
}
Solution 22 - Javascript
With ES6 (or using Babel or Typescipt) you can simply do:
var duplicates = myArray.filter(i => myArray.filter(ii => ii === i).length > 1);
Solution 23 - Javascript
I have just figured out a simple way to achieve this using an Array filter
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);
Solution 24 - Javascript
Simple code with ES6 syntax (return sorted array of duplicates):
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};
How to use:
duplicates([1,2,3,10,10,2,3,3,10]);
Solution 25 - Javascript
This answer might also be helpful, it leverages js reduce
operator/method to remove duplicates from array.
const result = [1, 2, 2, 3, 3, 3, 3].reduce((x, y) => x.includes(y) ? x : [...x, y], []);
console.log(result);
Solution 26 - Javascript
Higher ranked answers have a few inherent issues including the use of legacy javascript, incorrect ordering or with only support for 2 duplicated items.
Here's a modern solution which fixes those problems:
const arrayNonUniq = array => {
if (!Array.isArray(array)) {
throw new TypeError("An array must be provided!")
}
return array.filter((value, index) => array.indexOf(value) === index && array.lastIndexOf(value) !== index)
}
arrayNonUniq([1, 1, 2, 3, 3])
//=> [1, 3]
arrayNonUniq(["foo", "foo", "bar", "foo"])
//=> ['foo']
You can also use the npm package array-non-uniq
.
Solution 27 - Javascript
The following function (a variation of the eliminateDuplicates function already mentioned) seems to do the trick, returning test2,1,7,5 for the input ["test", "test2", "test2", 1, 1, 1, 2, 3, 4, 5, 6, 7, 7, 10, 22, 43, 1, 5, 8]
Note that the problem is stranger in JavaScript than in most other languages, because a JavaScript array can hold just about anything. Note that solutions that use sorting might need to provide an appropriate sorting function--I haven't tried that route yet.
This particular implementation works for (at least) strings and numbers.
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;
}
Solution 28 - Javascript
ES5 only (i.e., it needs a filter() polyfill for IE8 and below):
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] );
});
Solution 29 - Javascript
This is probably one of the fastest way to remove permanently the duplicates from an array 10x times faster than the most functions here.& 78x faster in safari
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);
- Test: http://jsperf.com/wgu
- Demo: http://jsfiddle.net/46S7g/
- More: https://stackoverflow.com/a/25082874/2450730
if you can't read the code above ask, read a javascript book or here are some explainations about shorter code. https://stackoverflow.com/a/21353032/2450730
EDIT
As stated in the comments this function does return an array with uniques, the question however asks to find the duplicates. in that case a simple modification to this function allows to push the duplicates into an array, then using the previous function toUnique
removes the duplicates of the duplicates.
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));
Solution 30 - Javascript
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;
}, []);
}
alert(returnDuplicates(arr));
This function avoids the sorting step and uses the reduce() method to push duplicates to a new array if it doesn't already exist in it.
Solution 31 - Javascript
Using "includes" to test if the element already exists.
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>
Solution 32 - Javascript
ES6 offers the Set data structure which is basically an array that doesn't accept duplicates. With the Set data structure, there's a very easy way to find duplicates in an array (using only one loop).
Here's my code
function findDuplicate(arr) {
var set = new Set();
var duplicates = new Set();
for (let i = 0; i< arr.length; i++) {
var size = set.size;
set.add(arr[i]);
if (set.size === size) {
duplicates.add(arr[i]);
}
}
return duplicates;
}
Solution 33 - Javascript
Following logic will be easier and faster
// @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));
}
Advantages :
- Single line :-P
- All inbuilt data structure helping in improving the efficiency
- Faster
Description of Logic :
- Converting to set to remove all duplicates
- Iterating through the set values
- With each set value check in the source array for the condition "values first index is not equal to the last index" == > Then inferred as duplicate else it is 'unique'
Note: map() and filter() methods are efficient and faster.
Solution 34 - Javascript
Just to add some theory to the above.
Finding duplicates has a lower bound of O(n*log(n) in the comparison model. SO theoretically, you cannot do any better than first sorting then going through the list sequentially removing any duplicates you find.
If you want to find the duplicates in linear (O(n)) expected time, you could hash each element of the list; if there is a collision, remove/label it as a duplicate, and continue.
Solution 35 - Javascript
I prefer the function way of doing this.
function removeDuplicates(links) {
return _.reduce(links, function(list, elem) {
if (list.indexOf(elem) == -1) {
list.push(elem);
}
return list;
}, []);
}
This uses underscore, but Array has a reduce
function, too
Solution 36 - Javascript
var input = ['a', 'b', 'a', 'c', 'c'],
duplicates = [],
i, j;
for (i = 0, j = input.length; i < j; i++) {
if (duplicates.indexOf(input[i]) === -1 && input.indexOf(input[i], i+1) !== -1) {
duplicates.push(input[i]);
}
}
console.log(duplicates);
Solution 37 - Javascript
I think the below is the easiest and fastest O(n) way to accomplish exactly what you asked:
function getDuplicates( arr ) {
var i, value;
var all = {};
var duplicates = [];
for( i=0; i<arr.length; i++ ) {
value = arr[i];
if( all[value] ) {
duplicates.push( value );
all[value] = false;
} else if( typeof all[value] == "undefined" ) {
all[value] = true;
}
}
return duplicates;
}
Or for ES5 or greater:
function getDuplicates( arr ) {
var all = {};
return arr.reduce(function( duplicates, value ) {
if( all[value] ) {
duplicates.push(value);
all[value] = false;
} else if( typeof all[value] == "undefined" ) {
all[value] = true;
}
return duplicates;
}, []);
}
Solution 38 - Javascript
Modifying @RaphaelMontanaro's solution, borrowing from @Nosredna's blog, here is what you could do if you just want to identify the duplicate elements from your array.
function identifyDuplicatesFromArray(arr) {
var i;
var len = arr.length;
var obj = {};
var duplicates = [];
for (i = 0; i < len; i++) {
if (!obj[arr[i]]) {
obj[arr[i]] = {};
}
else
{
duplicates.push(arr[i]);
}
}
return duplicates;
}
Thanks for the elegant solution, @Nosredna!
Solution 39 - Javascript
I did not like most answers.
Why? Too complicated, too much code, inefficient code and many do not answer the question, which is to find the duplicates (and not to give an array without the duplicates).
Next function returns all duplicates:
function GetDuplicates(arr) {
var i, out=[], obj={};
for (i=0; i < arr.length; i++)
obj[arr[i]] == undefined ? obj[arr[i]] ++ : out.push(arr[i]);
return out;
}
Because most of the time it is of no use to return ALL duplicates, but just to tell which duplicate values exist. In that case you return an array with unique duplicates ;-)
function GetDuplicates(arr) {
var i, out=[], obj={};
for (i=0; i < arr.length; i++)
obj[arr[i]] == undefined ? obj[arr[i]] ++ : out.push(arr[i]);
return GetUnique(out);
}
function GetUnique(arr) {
return $.grep(arr, function(elem, index) {
return index == $.inArray(elem, arr);
});
}
Maybe somebody else thinks the same.
Solution 40 - Javascript
To solve the above in O(n) time complexity (without sorting).
var arr = [9, 9, 111, 2, 3, 4, 4, 5, 7];
var obj={};
for(var i=0;i<arr.length;i++){
if(!obj[arr[i]]){
obj[arr[i]]=1;
} else {
obj[arr[i]]=obj[arr[i]]+1;
}
}
var result=[]
for(var key in obj){
if(obj[key]>1){
result.push(Number(key)) // change this to result.push(key) to find duplicate strings in an array
}
}
console.log(result)
Solution 41 - Javascript
function GetDuplicates(arr) {
var i = 0, m = [];
return arr.filter(function (n) {
return !m[n] * ~arr.indexOf(n, m[n] = ++i);
});
}
Solution 42 - Javascript
I feel like the simplest solution would to just use indexOf
full example of pushing only unique elements to an array.
var arr = ['a','b','c','d','a','b','c','d'];
var newA = [];
for(var i = 0; i < arr.length; i++){
if(newA.indexOf(arr[i]) === -1){
newA.push(arr[i]);
}
}
Solution 43 - Javascript
Magic
a.filter(( t={}, e=>!(1-(t[e]=++t[e]|0)) ))
O(n) performance; we assume your array is in a
and it contains elements that can be cast .toString()
in unique way (which is done implicity by JS in t[e]
) e.g numbers=[4,5,4], strings=["aa","bb","aa"], arraysNum=[[1,2,3], [43,2,3],[1,2,3]]. Explanation here, unique values here
var a1 = [[2, 17], [2, 17], [2, 17], [1, 12], [5, 9], [1, 12], [6, 2], [1, 12]];
var a2 = ['Mike', 'Adam','Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl'];
var a3 = [5,6,4,9,2,3,5,3,4,1,5,4,9];
let nd = (a) => a.filter((t={},e=>!(1-(t[e]=++t[e]|0))))
// Print
let c= x => console.log(JSON.stringify(x));
c( nd(a1) );
c( nd(a2) );
c( nd(a3) );
Solution 44 - Javascript
There is a really simple way to solve this. If you use the newish 'Set' javascript command. Set can take an array as input and output a new 'Set' that only contains unique values. Then by comparing the length of the array and the 'size' property of the set you can see if they differ. If they differ it must be due to a duplicate entry.
var array1 = ['value1','value2','value3','value1']; // contains duplicates
var array2 = ['value1','value2','value3','value4']; // unique values
console.log('array1 contains duplicates = ' + containsDuplicates(array1));
console.log('array2 contains duplicates = ' + containsDuplicates(array2));
function containsDuplicates(passedArray) {
let mySet = new Set(passedArray);
if (mySet.size !== passedArray.length) {
return true;
}
return false;
}
If you run the above snippet you will get this output.
array1 contains duplicates = true
array2 contains duplicates = false
Solution 45 - Javascript
Yet another way by using underscore. Numbers is the source array and dupes has possible duplicate values.
var itemcounts = _.countBy(numbers, function (n) { return n; });
var dupes = _.reduce(itemcounts, function (memo, item, idx) {
if (item > 1)
memo.push(idx);
return memo;
}, []);
Solution 46 - Javascript
Similar to a few other answers, but I used forEach()
to make it a bit prettier:
function find_duplicates(data) {
var track = {};
var duplicates = [];
data.forEach(function (item) {
!track[item] ? track[item] = true : duplicates.push(item);
});
return duplicates;
}
If a value is duplicated more than once, all its duplicates are returned, like so:
find_duplicates(['foo', 'foo', 'bar', 'bar', 'bar']);
// returns ['foo', 'bar', 'bar']
This might be what you want, otherwise you just have to follow with an "unique" filtering.
Solution 47 - Javascript
The quickest way to solve is is actually with a flag
var values = [4,2,3,1,4]
// solution
const checkDuplicate = list => {
var hasDuplicate = false;
list.sort().sort((a, b) => {
if (a === b) hasDuplicate = true
})
return hasDuplicate
}
console.log(checkDuplicate(values))
Solution 48 - Javascript
This can also be solved using Set()
.
A value in the Set may only occur once; it is unique in the Set's collection.
Array.prototype.hasDuplicates = function () {
if (arr.length !== (new Set(arr).size)) {
return true;
}
return false;
}
More information on Sets: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
Note: Sets are not fully supported in IE.
Solution 49 - Javascript
This is one of the simple ES5 solution I could think of -
function duplicates(arr) {
var duplicatesArr = [],
uniqueObj = {};
for (var i = 0; i < arr.length; i++) {
if( uniqueObj.hasOwnProperty(arr[i]) && duplicatesArr.indexOf( arr[i] ) === -1) {
duplicatesArr.push( arr[i] );
}
else {
uniqueObj[ arr[i] ] = true;
}
}
return duplicatesArr;
}
/* Input Arr: [1,1,2,2,2,1,3,4,5,3] */
/* OutPut Arr: [1,2,3] */
Solution 50 - Javascript
//find duplicates:
//sort, then reduce - concat values equal previous element, skip others
//input
var a = [1, 2, 3, 1, 2, 1, 2]
//short version:
var duplicates = a.sort().reduce((d, v, i, a) => i && v === a[i - 1] ? d.concat(v) : d, [])
console.log(duplicates); //[1, 1, 2, 2]
//readable version:
var duplicates = a.sort().reduce((output, element, index, input) => {
if ((index > 0) && (element === input[index - 1]))
return output.concat(element)
return output
}, [])
console.log(duplicates); //[1, 1, 2, 2]
Solution 51 - Javascript
-
Printing duplicate values
var arr = [1,2,3,4,13,2,3,4,3,4];
// non_unique Printing
function nonUnique(arr){
var result = [];
for(var i =0;i<arr.length;i++){
if(arr.indexOf(arr[i],i+1) > -1){
result.push(arr[i]);
}
}
console.log(result);
}nonUnique(arr);
// unique Printing
function uniqueDuplicateVal(arr){
var result = [];
for(var i =0;i<arr.length;i++){
if(arr.indexOf(arr[i],i+1) > -1){
if(result.indexOf(arr[i]) === -1]){
result.push(arr[i]);
}
}
}
}
uniqueDuplicateVal(arr)
Solution 52 - Javascript
This should be one of the shortest and easiest ways to actually find duplicate values in an array.
var arr = [1,2,3,4,5,6,7,8,1,2,3,4,5,3,3,4];
var data = arr.filter(function(item,index,arr){
return arr.indexOf(item) != arr.lastIndexOf(item) && arr.indexOf(item) == index;
})
console.log(data );
Solution 53 - Javascript
This is most efficient way i can think of as doesn't include Array.indexOf()
or Array.lastIndexOf()
which have complexity of O(n) and using inside any loop of complexity O(n) will make complete complexity O(n^2).
My first loop have complexity of O(n/2) or O((n/2) + 1), as complexity of search in hash is O(1). The second loop worst complexity when there's no duplicate in array is O(n) and best complexity when every element have a duplicate is O(n/2).
function duplicates(arr) {
let duplicates = [],
d = {},
i = 0,
j = arr.length - 1;
// Complexity O(n/2)
while (i <= j) {
if (i === j)
d[arr[i]] ? d[arr[i]] += 1 : d[arr[i]] = 1; // Complexity O(1)
else {
d[arr[i]] ? d[arr[i]] += 1 : d[arr[i]] = 1; // Complexity O(1)
d[arr[j]] ? d[arr[j]] += 1 : d[arr[j]] = 1; // Complexity O(1)
}
++i;
--j;
}
// Worst complexity O(n), best complexity O(n/2)
for (let k in d) {
if (d[k] > 1)
duplicates.push(k);
}
return duplicates;
}
console.log(duplicates([5,6,4,9,2,3,5,3,4,1,5,4,9]));
console.log(duplicates([2,3,4,5,4,3,4]));
console.log(duplicates([4,5,2,9]));
console.log(duplicates([4,5,2,9,2,5,9,4]));
Solution 54 - Javascript
This is a single loop approach with a hash table for counting the elements and filter the array if the count is 2
, because it returns the first found duplicate.
Advantage:
- single loop
- uses an object for counting in a closure
var array = [5, 0, 2, 1, 2, 3, 3, 4, 4, 8, 6, 7, 9, 4], duplicates = array.filter((h => v => (h[v] = (h[v] || 0) + 1) === 2)({}));
console.log(duplicates);
Solution 55 - Javascript
You can make use of the sort
, filter
and sets
to do that.
var numbers = [1,2,3,4,5,6,7,8,1,2,3,4,5,3,3,4];
var numbersSorted = numbers.sort();
let result = numbers.filter((e, i) => numbers[i] == numbers[i+1]);
result = [...new Set(result)];
console.log(result);
Solution 56 - Javascript
Based on @bluemoon but shorter, returns all duplicates exactly once!
function checkDuplicateKeys(arr) {
const counts = {}
return arr.filter((item) => {
counts[item] = counts[item] || 1
if (counts[item]++ === 2) return true
})
}
// [1,2,2,2,2,2,2] => [1,2]
// ['dog', 'dog', 'cat'] => ['dog']
Solution 57 - Javascript
const names = [
"Alex",
"Matt",
12,
"You",
"Me",
12,
"Carol",
"Bike",
"Carol",
];
const count = (names) =>
names.reduce((a, b) => ({ ...a, [b]: (a[b] || 0) + 1 }), {});
let obj = count(names);
let objectKeys = Object.keys(obj);
let repetitiveElements = [];
let answer = objectKeys.map((value) => {
if (obj[value] > 1) {
return repetitiveElements.push(value);
}
});
console.log(repetitiveElements);
Solution 58 - Javascript
There are so many answers already, but unfortunately some are too long, some are short but too cryptic for me while others are beyond my scope of knowledge... I really like this solution that I've come up with, though. Hope it's still helpful for some!
Even though the original post says he/she doesn't actually need the duplicates' indexes or how many times they are duplicated, I think it's still clearest to have 'em counted.
Codes with notes.
function findDuplicates(array, count = {}) {
// with count declared in the parameter, initialized as an empty object,
// it can store the counts of all elements in array
// using the forEach loop to iterate through the input array,
// also using the conditional ternary operators
// (works just like a normal if-else statement, but just a bit cleaner)
// we can store all occurrences of each element from array in count
array.forEach(el => count[el] ? count[el]++ : count[el] = 1)
// using Object.keys, we get an array of all keys from count (all numbers)
// (sorted as well, though of no specific importance here)
// using filter to find all elements with a count (value) > 1 (duplicates!)
return Object.keys(count).filter(key => count[key] > 1);
}
Just the codes (with test cases).
function findDuplicates(array, count = {}) {
array.forEach(el => count[el] ? count[el]++ : count[el] = 1);
return Object.keys(count).filter(key => count[key] > 1);
}
let arr1 = [9, 9, 111, 2, 3, 4, 4, 5, 7];
let arr2 = [1,6,7,3,6,8,1,3,4,5,1,7,2,6];
console.log(findDuplicates(arr1)); // => ['4', '9']
console.log(findDuplicates(arr2)); // => ['1', '3', '6', '7']
Solution 59 - Javascript
It's actually a shame that this question has so many wrong answers or answers which need a lot of extra memory like a Set. Clean and simple solution:
function findDuplicates<T>(arr: Array<T>): T[] {
//If the array has less than 2 elements there are no duplicates
const n = arr.length;
if (n < 2)
return [];
const sorted = arr.sort();
const result = [];
//Head
if (sorted[0] === sorted[1])
result.push(sorted[0]);
//Inner (Head :: Inner :: Tail)
for (let i = 1; i < n-1; i++) {
const elem = sorted[i];
if (elem === sorted[i - 1] || elem === sorted[i+1])
result.push(elem)
}
//Tail
if (sorted[n - 1] == sorted[n - 2])
result.push(sorted[n - 1]);
return result;
}
console.dir(findDuplicates(['a', 'a', 'b', 'b']));
console.dir(findDuplicates(['a', 'b']));
console.dir(findDuplicates(['a', 'a', 'a']));
console.dir(findDuplicates(['a']));
console.dir(findDuplicates([]));
Solution 60 - Javascript
The Prototype library has a uniq function, which returns the array without the dupes. That's only half of the work though.
Solution 61 - Javascript
/* The indexOf method of the Array object is useful for comparing array items. IE is the only major browser that does not natively support it, but it is easy to implement: */
Array.prototype.indexOf= Array.prototype.indexOf || function(what, i){
i= i || 0;
var L= this.length;
while(i<L){
if(this[i]=== what) return i;
++i;
}
return -1;
}
function getarrayduplicates(arg){
var itm, A= arg.slice(0, arg.length), dups= [];
while(A.length){
itm= A.shift();
if(A.indexOf(itm)!= -1 && dups.indexOf(itm)== -1){
dups[dups.length]= itm;
}
}
return dups;
}
var a1= [1, 22, 3, 2, 2, 3, 3, 4, 1, 22, 7, 8, 9];
alert(getarrayduplicates(a1));
For very large arrays, it can be faster to remove the duplicates from the array as they are found, so that they will not be looked at again:
function getarrayduplicates(arg){
var itm, A= arg.slice(0, arg.length), dups= [];
while(A.length){
itm= A.shift();
if(A.indexOf(itm)!= -1){
dups[dups.length]= itm;
while(A.indexOf(itm)!= -1){
A.splice(A.indexOf(itm), 1);
}
}
}
return dups;
}
Solution 62 - Javascript
From Raphael Montanaro answer, it can improve to use with array/object item as follows.
function eliminateDuplicates(arr) {
var len = arr.length,
out = [],
obj = {};
for (var key, i=0; i < len; i++) {
key = JSON.stringify(arr[i]);
obj[key] = (obj[key]) ? obj[key] + 1 : 1;
}
for (var key in obj) {
out.push(JSON.parse(key));
}
return [out, obj];
}
Note: You need to use JSON library for browser that's not supported JSON.
Solution 63 - Javascript
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 placehash[<value>].length > 1
, signifies a duplicate. -
hash['hello']
will return[0,3]
because 'hello' was found in node 0 and 3 inarr[]
.
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.
Solution 64 - Javascript
Here is the one of methods to avoid duplicates into javascript array...and it supports for strings and numbers...
var unique = function(origArr) {
var newArray = [],
origLen = origArr.length,
found,
x = 0; y = 0;
for ( x = 0; x < origLen; x++ ) {
found = undefined;
for ( y = 0; y < newArray.length; y++ ) {
if ( origArr[x] === newArray[y] ) found = true;
}
if ( !found) newArray.push( origArr[x] );
}
return newArray;
}
check this fiddle..
Solution 65 - Javascript
I am trying to improve the answer from @swilliams, this will return an array without duplicates.
// arrays for testing
var arr = [9, 9, 111, 2, 3, 4, 4, 5, 7];
// ascending order
var sorted_arr = arr.sort(function(a,b){return a-b;});
var arr_length = arr.length;
var results = [];
if(arr_length){
if(arr_length == 1){
results = arr;
}else{
for (var i = 0; i < arr.length - 1; i++) {
if (sorted_arr[i + 1] != sorted_arr[i]) {
results.push(sorted_arr[i]);
}
// for last element
if (i == arr.length - 2){
results.push(sorted_arr[i+1]);
}
}
}
}
alert(results);
Solution 66 - Javascript
Here is one implemented using sort() and JSON.stringify()
https://gist.github.com/korczis/7598657
function removeDuplicates(vals) {
var res = [];
var tmp = vals.sort();
for (var i = 0; i < tmp.length; i++) {
res.push(tmp[i]);
while (JSON.stringify(tmp[i]) == JSON.stringify(tmp[i + 1])) {
i++;
}
}
return res;
}
console.log(removeDuplicates([1,2,3,4,5,4,3,3,2,1,]));
Solution 67 - Javascript
here is a small simple snippet to find unique and duplicate values with out sorting and two loops.
var _unique = function (arr) {
var h = [], t = [];
arr.forEach(function (n) {
if (h.indexOf(n) == -1)
h.push(n);
else t.push(n);
});
return [h, t];
}
var result = _unique(["test",1,4,2,34,6,21,3,4,"test","prince","th",34]);
console.log("Has duplicate values : " + (result[1].length > 0)) //and you can check count of duplicate values
console.log(result[0]) //unique values
console.log(result[1]) //duplicate values
Solution 68 - Javascript
You can use the following construction:
var arr = [1,2,3,4,5,6,7,8,9,0,5];
var duplicate = arr.filter(function(item, i, arr) {
return -1 !== arr.indexOf(item, i + 1);
})
Solution 69 - Javascript
using Pure Js
function arr(){
var a= [1,2,3,4,5,34,2,5,7,8,6,4,3,25,8,34,56,7,8,76,4,23,34,46,575,8564,53,5345657566];
var b= [];
b.push(a[0]);
var z=0;
for(var i=0; i< a.length; i++){
for(var j=0; j< b.length; j++){
if(b[j] == a[i]){
z = 0;
break;
}
else
z = 1;
}
if(z)
b.push(a[i]);
}
console.log(b);
}
Solution 70 - Javascript
var isUnique = true;
for (var i= 0; i< targetItems.length; i++) {
var itemValue = $(targetItems[i]).val();
if (targetListValues.indexOf(itemValue) >= 0) {
isUnique = false;
break;
}
targetListValues.push(itemValue);
if (!isUnique) {
//raise any error msg
return false;
}
}
Solution 71 - Javascript
Here we output the duplicates just once per dupe.
var arr = [9, 9, 9, 9, 111, 2, 3, 4, 4, 5, 7];
arr.sort();
var results = [];
for (var i = 0; i < arr.length - 1; i++) {
if (arr[i + 1] == arr[i]) {
results.push(arr[i]);
}
}
results = Array.from(new Set(results))
console.log(results);
Solution 72 - Javascript
This is more advanced function based on number of occurences.
function getMostDuplicated(arr, count) {
const result = [];
arr.forEach((item) => {
let i = 0;
arr.forEach((checkTo) => {
if (item === checkTo) {
i++;
if (i === count && result.indexOf(item) === -1) {
result.push(item);
}
}
});
});
return result;
}
arr = [1,1,1,2,5,67,3,2,3,2,3,1,2,3,4,1,4];
result = getMostDuplicated(arr, 5); // result is 1
result = getMostDuplicated(arr, 2); // result 1, 2, 3, 4
console.log(result);
Solution 73 - Javascript
This is how I implemented it with map. It should run in O(n) time and should kinda be easy to gasp.
var first_array=[1,1,2,3,4,4,5,6];
var find_dup=new Map;
for (const iterator of first_array) {
// if present value++
if(find_dup.has(iterator)){
find_dup.set(iterator,find_dup.get(iterator)+1);
}else{
// else add it
find_dup.set(iterator,1);
}
}
console.log(find_dup.get(2));
Then you can find_dup.get(key)
to find if it has duplicates (it should give > 1).
Solution 74 - Javascript
Returns duplicates and preserves data type.
With O(4n) performance
const dupes = arr => {
const map = arr.reduce((map, curr) => {
return (map.set(curr, (map.get(curr) || 0) + 1), map)
}, new Map());
return Array.from(map).filter(([key, val])=> val > 1).map(([key, val]) => key)
}
With O(2n) performance
const dupes = arr => {
const map = arr.reduce((map, curr) => {
return (map.set(curr, (map.get(curr) || 0) + 1), map)
}, new Map());
const dupes_ = [];
for (let [key, val] of map.entries()) {
if (val > 1) dupes_.push(key);
}
return dupes_;
}
Solution 75 - Javascript
Simplest way to fetch duplicates/repeated values from array/string :
function getDuplicates(param) {
var duplicates = {}
for (var i = 0; i < param.length; i++) {
var char = param[i]
if (duplicates[char]) {
duplicates[char]++
} else {
duplicates[char] = 1
}
}
return duplicates
}
console.log(getDuplicates("aeiouaeiou"));
console.log(getDuplicates(["a", "e", "i", "o", "u", "a", "e"]));
console.log(getDuplicates([1, 2, 3, 4, 5, 1, 1, 2, 3]));
Solution 76 - Javascript
This will return duplicates from an Array as an Array of duplicates.
const duplicates = function(arr) {
// let try moving in pairs.. maybe that will work
let dups = new Set(),
r = []
arr.sort()
arr.reduce((pv, cv) => {
if (pv === cv) {
dups.add(pv)
}
return cv
})
for (let m of dups.values()) {
r.push(m)
}
return r
}
console.log(duplicates([1,3,5,6,7,4,4,5,1,4,6,3,8,9,5,0]))
Solution 77 - Javascript
Very simple way:
function getDuplicateValues(someArray) {
const duplicateValues = new Set([])
const check = new Set([])
someArray.forEach(v => {
if (check.has(v)) {
duplicateValues.add(v)
} else {
check.add(v)
}
})
return Array.from(duplicateValues);
}
const result = getDuplicateValues(['coffee', 'soda', 'water', 'juice', 'water', 'water', 'coffee'])
repeated_values.textContent = JSON.stringify(result, null, ' ')
<pre id="repeated_values"></pre>
Solution 78 - Javascript
The accepted answer is the most perfect one but as some users has pointed that for cases where an element is repeated more than 2 times it will gives us the array with repeated elements:
This solution covers that scenarios too::
const peoples = [
{id: 1, name:"Arjun"},
{id: 2, name:"quinze"},
{id: 3, name:"catorze"},
{id: 1, name:"Arjun"},
{id: 4, name:"dezesseis"},
{id: 1, name:"Arjun"},
{id: 2, name:"quinze"},
{id: 3, name:"catorzee"}
]
function repeated(ppl){
const newppl = ppl.slice().sort((a,b) => a.id -b.id);
let rept = [];
for(let i = 0; i < newppl.length-1 ; i++){
if (newppl[i+1].id == newppl[i].id){
rept.push(newppl[i+1]);
}
}
return [...new Set(rept.map(el => el.id))].map(rid =>
rept.find(el => el.id === rid)
);
}
repeated(peoples);
Solution 79 - Javascript
This was asked me in an interview, My answer is,
List<int> getDublicates(List<int> x)
{
List<int> result = new List<int>();
while (x.Count>0)
{
int d = x[0];
x.Remove(x[0]);
if (x.Contains(d))
result.Add(d);
}
return result;
}
it have good performance
Solution 80 - Javascript
The shortest way to remove duplicates is by using Set and Spread syntax
const remove = (array) => [...new Set(array)];
console.log(remove([1,1,2,2,3]); //1,2,3
Solution 81 - Javascript
[1, 2, 2, 3, 3, 4, 5, 6, 2, 3, 50, 8, 5, 22, 1, 2, 511, 12, 50, 22].reduce(function (total, currentValue, currentIndex, arr) {
if (total.indexOf(currentValue) === -1 && arr.indexOf(currentValue) !== currentIndex)
total.push(currentValue);
return total;
}, [])
Solution 82 - Javascript
const a = ['a', 'b', 'b']
function findDuplicates(a) {
return Object.keys(_.pickBy(_.countBy(a), x => x > 1))
}
Solution 83 - Javascript
You can use the following code to get the duplicate elements in a given array:
let name = ['satya', 'amit', 'aditya', 'abhay', 'satya', 'amit'];
let dup = [];
let uniq = [];
name.forEach((item, index) => {
if(!uniq.includes(item)) {
uniq[index] = item;
}
if (name.indexOf(item, index + 1) != -1) {
dup[index] = item;
}
})
Solution 84 - Javascript
Here's one without using a temp Array to store the non-duplicate ones:
// 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;
};
Solution 85 - Javascript
function remove_dups(arrayName){
var newArray = new Array();
label:for(var i=0; i<arrayName.length; i++ ){
for(var j=0; j<newArray.length;j++ ){
if(newArray[j]==arrayName[i]){
continue label;
}
}
newArray[newArray.length] = arrayName[i];
}
return newArray;
}
Solution 86 - Javascript
Surprised no one posted this solution
.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>
</title>
</head>
<body>
<script>
var list = [100,33,45,54,9,12,80,100];
var newObj = {};
var newArr = [];
for(var i=0; i<list.length; i++){
newObj[list[i]] = i;
}
for(var j in newObj){
newArr.push(j);
}
console.log(newArr);
</script>
</body>
</html>
Solution 87 - Javascript
var arr = [4,5,1,1,2,3,4,4,7,5,2,6,10,9];
var sorted_arr = arr.sort();
var len = arr.length;
var results = [];
for (var i = 0; i < len; i++) {
if (sorted_arr[i + 1] !== sorted_arr[i]) {
results.push(sorted_arr[i]);
}
}
document.write(results);
Solution 88 - Javascript
var a= [1, 2,2,3,3,4,4,4];
var m=[];
var n = [];
a.forEach(function(e) {
if(m.indexOf(e)=== -1) {
m.push(e);
}else if(n.indexOf(e)=== -1){
n.push(e);
}
});
Solution 89 - Javascript
You can proceed by comparing index:
function getDuplicate(array) {
return array.filter((value, index) => array.value !== index)
}
Solution 90 - Javascript
We will use Javascript ES6 Functionality to do magic!
var arr = [9, 9, 111, 2, 3, 4, 4, 5, 7];
const filtered = arr.filter((value, index) => {
return arr.indexOf(value) >= index;
});
console.log(filtered);
Solution 91 - Javascript
var arr = ['a','b','c','a'];
arr.filter( (item , index ) => {
console.log(item , index , arr.indexOf(item) , arr.indexOf( item ) == index);
return index == arr.indexOf(item)
} );
Solution 92 - Javascript
var array = ['a', 'b', 'c', 'a'];
function unique(array) {
var unique_arr = [];
array.forEach(function(i, e) {
if (unique_arr.indexOf(i)===-1) unique_arr.push(i);
});
return unique_arr;
}
console.log(unique(array));
Solution 93 - Javascript
In this post was useful for duplication check if u are using Jquery.
https://stackoverflow.com/questions/4346547/how-to-find-the-duplicates-in-an-array-using-jquery
var unique_values = {}; var list_of_values = []; $('input[name$="recordset"]'). each(function(item) { if ( ! unique_values[item.value] ) { unique_values[item.value] = true; list_of_values.push(item.value); } else { // We have duplicate values! } });
Solution 94 - Javascript
//program to find the duplicate elements in arraylist
import java.util.ArrayList;
import java.util.Scanner;
public class DistinctEle
{
public static void main(String args[])
{
System.out.println("Enter elements");
ArrayList<Integer> abc=new ArrayList<Integer>();
ArrayList<Integer> ab=new ArrayList<Integer>();
Scanner a=new Scanner(System.in);
int b;
for(int i=0;i<=10;i++)
{
b=a.nextInt();
if(!abc.contains(b))
{
abc.add(b);
}
else
{
System.out.println("duplicate elements"+b);
}
}
}
}