535

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:

Dharman
  • 30,962
  • 25
  • 85
  • 135
Scott Saunders
  • 29,840
  • 14
  • 57
  • 64
  • 29
    There seems to be years of confusion about what this question asks. I needed to know what elements in the array were duplicated: "I just need to find what the duplicated values are". The correct answer should NOT remove duplicates from the array. That's the inverse of what I wanted: a list of the duplicates, not a list of unique elements. – Scott Saunders Feb 22 '13 at 15:47
  • https://github.com/lodash/lodash/issues/4852#issuecomment-666366511 I would add this as an answer, but given the length of answers, it would never be seen – lonewarrior556 Jul 30 '20 at 14:44

98 Answers98

370

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

Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
swilliams
  • 48,060
  • 27
  • 100
  • 130
  • Emil, your edit was correct, thanks. :) I just say "Oh squared". – swilliams May 08 '09 at 17:10
  • 12
    "Assuming your sort algorithm is good, this should be less than O^2". Specifically, it could be O(n*log(n)). – ESRogs May 08 '09 at 17:20
  • And this is the best you can do in the comparison model. – SplittingField May 09 '09 at 00:13
  • @swilliams: What if the array is an array of html elements, how do you sort them 1st?! – Marco Demaio Aug 18 '10 at 18:28
  • 101
    This script doesn't work so well with more than 2 duplicates (e.g. `arr = [9, 9, 9, 111, 2, 3, 3, 3, 4, 4, 5, 7];` – Mottie Oct 23 '10 at 15:00
  • 1
    Instead of `i += 1` you can write `i++`. – Danilo Bargen Dec 22 '10 at 15:03
  • 3
    @danilo I know, though I tend to follow Crockford's advice on not using that pattern in JS. http://javascript.crockford.com/code.html – swilliams Dec 22 '10 at 18:12
  • 7
    @swilliams I don't think those guidelines say anything about not using `i++`. Instead, they say not to write `j = i + +j`. Two different things IMHO. I think `i += 1` is more confusing than the simple and beautiful `i++` :) – Danilo Bargen Dec 23 '10 at 14:31
  • The sample script currently alerts "4,9". Thats a huge failure. This thing is broken – Timo Tijhof Mar 10 '12 at 12:31
  • 1
    This script is broken, it assumes an array made entirely of duplicates (i.e. if you have a single value without a duplicate in the array the script will generate a wrong result) – Raphael Mar 13 '12 at 22:25
  • for long arr, you might consider removing arr.length from the head of the loop as on old browsers (specifically IE) this considerable drops the looping speed. – Joscha May 02 '12 at 17:07
  • This script is broken: the sort function for string compare *will not work* for numbers! Also you need another i++ for when...nevermind, I'll fix it. – gengkev May 23 '12 at 04:53
  • I rollbacked last edit because it was leading to an error with i=arr.length-1. – Denys Séguret Aug 22 '12 at 15:56
  • 37
    -1 This answer is wrong on many levels. First of all `var sorted_arr = arr.sort()` is useless: `arr.sort()` mutates the original array (which is a problem in its own right). This also discards an element. (Run the code above. What happens to 9?) cc @dystroy A cleaner solution would be `results = arr.filter(function(elem, pos) { return arr.indexOf(elem) == pos; })` – NullUserException Jan 13 '13 at 21:10
  • @NullUserException I'll take no responsibility for this answer : I just fixed the i+1 being to big. In fact I just rollbacked an edit. I know I answered (with many tests) another similar question but I don't have the link... – Denys Séguret Jan 13 '13 at 21:12
  • 26
    Everyone: the question asks to display the duplicate values, not to remove them. Please don't edit/break the code to try to make it do something it's not trying to do. The alert should show the values that are duplicated. – Scott Saunders Feb 22 '13 at 15:50
  • This is short, although sorting it is totally unnecessary. – Rafael Xavier May 23 '13 at 14:36
  • Although this isn't what the OP asked, Google brought me here when I was searching for a quick way to remove duplicate values from an array. So +1 anyway. – Kabir Sarin Mar 18 '14 at 04:33
  • 1
    Like Mottie mentioned above, this snippet doesn't know work when there is more than 2 duplicated values, ex array like this [3,3,3,1] will not work – angry kiwi Dec 30 '16 at 08:53
  • instead of looping through the sorted array you could use `sort((x,y)=>{if(x===y) duplicated.push(x)})` – Paweł Oct 16 '17 at 19:06
  • 1
    Maybe can be better if change the if to `if (sorted_arr[i + 1] == sorted_arr[i] && results[results.length-1]!=sorted_arr[i])` – Malus Jan May 25 '18 at 12:39
  • This does work. It gives back ALL duplicates sorted, which you might not want. There are better (simpeler, shorter) solutions like https://stackoverflow.com/a/31017485/760777 – RWC Nov 20 '18 at 11:36
215

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/

namgold
  • 1,009
  • 1
  • 11
  • 32
rapfaria
  • 2,577
  • 2
  • 14
  • 14
  • 27
    That is good code, but unfortunately it doesn't do what I'm asking for. – Scott Saunders May 08 '09 at 17:59
  • 71
    The code above (which is mine--that's my blog) gets you pretty close. A small tweak and you're there. First of all, you can see if arr.length and out.length are the same. If they are the same, there are no duplicated elements. But you want a little more. If you want to "catch" the dupes as they happen, check to see if the length of the array increases after the obj[arr[i]]=0 line. Nifty, eh? :-) Thanks for the nice words, Raphael Montanaro. – Nosredna May 08 '09 at 22:25
  • Also turns Numbers into Strings, which may or may not be acceptable depending on what you're doing. Nice trick however as long as one understands the gotchas – adamJLev Oct 27 '10 at 22:46
  • 6
    @MarcoDemaio: Uh, no, why would the code not work with spaces? You can put whatever you like in a property name - just can't use the dot syntax to access ones with spaces (nor props with various other characters which would break parsing). – Gijs Oct 11 '11 at 10:29
  • 4
    @Gijs: +1 you are right. I didn't know it. But it still does not work when it's an array of objects. – Marco Demaio Oct 16 '11 at 12:19
  • 3
    This algorithm also has the side effect of returning a sorted array, which might not be what you want. – asymmetric Jun 03 '12 at 19:26
  • 1
    This won't work if you're trying to remove duplicate objects from an array of objects. – Ben Lesh Dec 17 '12 at 16:23
  • 2
    Do you want a *really* elegant way to remove duplicates? Try this one liner: http://stackoverflow.com/a/9229821/396458 – NullUserException Jan 11 '13 at 07:31
  • @asymmetric follow below my answer that returns the duplicates in the same order. att – Rafael Xavier May 23 '13 at 14:30
  • as a one-liner, also handling cases were an item occurs more often than twice: arr.slice().sort().filter((e, i, a) => a[i + 1] === e && a[i - 1] !== e) – fholzer Oct 10 '17 at 11:54
  • @vsync Your assertion is wrong, I can make a single loop slower than two loops. –  Mar 02 '19 at 22:27
  • @vsync I mean, your assertion is naive. Actually, the time it takes to run an algorithm mainly depends on the size of the input. This answer is O(n), which is not so bad. Read https://en.m.wikipedia.org/wiki/Time_complexity. –  Mar 03 '19 at 06:43
206

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' ]
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Christian Landgren
  • 13,127
  • 6
  • 35
  • 31
152

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]

Note that this answer’s main goal is to be short. If you need something performant for a big array, one possible solution is to sort your array first (if it is sortable) then do the following to get the same kind of results as above:

myHugeSortedArray.filter((e, i, a) => a[i-1] === e)

Here is an example for a 1 000 000 integers array:

const myHugeIntArrayWithDuplicates =
  [...Array(1_000_000).keys()]
  // adding two 0 and four 9 duplicates
  .fill(0, 2, 4).fill(9, 10, 14)

console.time("time")
console.log(
  myHugeIntArrayWithDuplicates
  // a possible sorting method for integers
  .sort((a, b) => a > b ? 1 : -1)
  .filter((e, i, a) => a[i-1] === e)
)
console.timeEnd("time")

On my AMD Ryzen 7 5700G dev machine it outputs:

[ 0, 0, 9, 9, 9, 9 ]
time: 22.738ms

As pointed out in the comments both the short solution and the performant solution will return an array with several time the same duplicate if it occurs more than once in the original array:

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

If unique duplicates are wanted then a function like

function duplicates(arr) {
  return [...new Set(arr.filter((e, i, a) => a.indexOf(e) !== i))]
}

can be used so that duplicates([1, 1, 1, 2, 2, 2, 2]) returns [1, 2].


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.

Laurent Payot
  • 2,048
  • 2
  • 17
  • 13
  • 6
    Remember: `[2,2,2,2].filter((e, i, a) => a.indexOf(e) !== i)` gives `[2, 2, 2]` – Wajahath Apr 17 '21 at 10:01
  • 6
    @Wajahath That's true, thanks for pointing that out. If unique duplicates are wanted then a function like `f = arr => [...new Set(arr.filter((e, i, a) => a.indexOf(e) !== i))]` can be used so that `f([1, 1, 1, 2, 2, 2, 2])` returns `[1, 2]` – Laurent Payot Apr 18 '21 at 05:46
  • performance wise, this is really bad if you have like 1 000 000 entries – strix25 Oct 05 '22 at 10:21
  • 1
    @strix25 that’s true, I added a similar solution that performs much better on a 1 000 000 _sorted_ array. – Laurent Payot Oct 07 '22 at 09:09
77

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])

// Or, using Sets (about 4 times faster)

var duplicates = Array.from(items.reduce((acc, v, i, arr) {
  return arr.indexOf(v) !== i ? acc.add(v) : acc;
}, new Set()))

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.

Because of reduce() and indexOf() it needs at least IE 9.

meetar
  • 7,443
  • 8
  • 42
  • 73
flu
  • 14,307
  • 8
  • 74
  • 71
  • 11
    ES6 arrow/simple/pure version: `const dupes = items.reduce((acc, v, i, arr) => arr.indexOf(v) !== i && acc.indexOf(v) === -1 ? acc.concat(v) : acc, [])` – ZephDavies Nov 29 '18 at 14:56
  • `if (arr.indexOf(el) !== i && !acc.includes(el) ) acc.push(el); return acc;` works too – Kopi Bryant Oct 12 '22 at 14:46
30

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);
karim79
  • 339,989
  • 67
  • 413
  • 406
28

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;
    }
  });
}
  • really? why answer a question which has been solved over 2 years ago? – Rene Pot Oct 28 '11 at 11:16
  • 3
    I was answering another question and apparently accidentally clicked on someone linking to this one, calling it a duplicate, and ended up cloning my answer and confusing the hell out of myself. I edit my stuff a lot. –  Oct 28 '11 at 11:19
  • http://stackoverflow.com/questions/7683845/removing-duplicates-from-an-array-in-javascript –  Oct 28 '11 at 11:19
  • 17
    I think it's nice with different solutions. It doesn't matter that the topic is old and solved since it's still possible to come up with different ways of doing this. It's a typical problem in computer science. – Emil Vikström Nov 30 '11 at 12:53
  • You might want to mention that this relies on ES5 Array methods that aren't implemented in IE < 9. – Tim Down May 01 '12 at 11:39
  • Why don't you use a simple counter for the `uid` function? – Bergi Oct 04 '12 at 18:25
24
var a = ["a","a","b","c","c"];

a.filter(function(value,index,self){ return (self.indexOf(value) !== index )})
  • This seems to work, but you should probably include some text describing how it works. – The DIMM Reaper Aug 20 '15 at 15:27
  • 4
    Won't work if there are more 2 occurrences of a duplicate value. – vasa Nov 03 '15 at 00:37
  • 1
    This is elegant and simple. I love it. For those wanting to figure out how they work, I've created a gist showing how to show duplicates and eliminate duplicates. See here: https://gist.github.com/jbcoder/f1c616a32ee4d642691792eebdc4257b – Josh Jul 22 '16 at 17:40
  • @TheDIMMReaper for the second `'a'` in array, inside filter function the `index == 1`, whereas `self.indexOf('a') == 0` – Sergiy Ostrovsky Nov 16 '18 at 13:40
23

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]
vsync
  • 118,978
  • 58
  • 307
  • 400
  • +1 because it's definitely more readable the code using Array.indexOf, but unfortunately it seems slower than using a simple nested loop. Even on browsers that implements Array.indexOf nayively like FF. Plz, Have a look at these tests I did here: http://jsperf.com/array-unique2 and let me know your thoughts. – Marco Demaio Mar 04 '11 at 16:41
  • @shekhardesigner - updated answer. "r" is the array you search in – vsync Feb 20 '14 at 14:40
  • @vsync I had to initialize, `var r = [];` to get your code working. And worked like charm. – absqueued Feb 21 '14 at 12:06
  • @shekhardesigner - I'm sorry for the mix, for the Array Prototype solution you don't need an `r` variable – vsync Feb 21 '14 at 12:40
  • 2
    Does not do what the OP asked for, return the duplicates. – RWC Aug 18 '17 at 12:16
  • @RWC - it appears so. I will update the answer in the coming days to best suit the OP's question – vsync Aug 18 '17 at 13:39
  • I had to change `this.sort()` to `this.slice().sort()` for the PRE-ES2015 one to not affect the orginal array. Otherwise good answer. – Ste Jun 04 '21 at 17:08
22

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.
bluesmoon
  • 3,918
  • 3
  • 25
  • 30
Daniel Beardsley
  • 19,907
  • 21
  • 66
  • 79
14

using underscore.js

function hasDuplicate(arr){
    return (arr.length != _.uniq(arr).length);
}
Marco Allori
  • 3,198
  • 33
  • 25
14

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);
        return false;
    } else {
        return true;
    }
});

// OR more concisely

const duplicates = numbers.filter(item => !set.delete(item));

console.log(duplicates);
// [ 2, 5 ]
nebkat
  • 8,445
  • 9
  • 41
  • 60
attacomsian
  • 2,667
  • 23
  • 24
  • Good clear algorithm. By the way, this reports triplicate elements twice, which may well be desirable. – Bob Stein Dec 22 '21 at 21:11
  • An improvement on this would be `item => !set.delete(item)`, as Set.delete() return false if the item was not in the set. – nebkat Nov 08 '22 at 12:16
9

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]
lukaszkups
  • 5,790
  • 9
  • 47
  • 85
9

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:

  1. It works with any numbers including 0, strings and negative numbers e.g. -1 - Related question: Get all unique values in a JavaScript array (remove duplicates)

  2. The original array arr is preserved (filter returns the new array instead of modifying the original)

  3. 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' ])

  4. 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.

nircraft
  • 8,242
  • 5
  • 30
  • 46
8

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))]
);
Oleg Abrazhaev
  • 2,751
  • 2
  • 28
  • 41
7

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
sravan ganji
  • 4,774
  • 3
  • 25
  • 37
7

Shortest vanilla JS:

[1,1,2,2,2,3].filter((v,i,a) => a.indexOf(v) !== i) // [1, 2, 2]
chickens
  • 19,976
  • 6
  • 58
  • 55
6

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']
Sivakumar Tadisetti
  • 4,865
  • 7
  • 34
  • 56
Lucas Janon
  • 1,502
  • 1
  • 15
  • 18
6
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

mflodin
  • 1,093
  • 1
  • 12
  • 22
Lorenz Lo Sauer
  • 23,698
  • 16
  • 85
  • 87
  • 1
    The filter function should return true or false, not the element itself. Filtering an array containing 0's would not have returned them. – mflodin Jan 09 '12 at 23:43
  • Also, I assume the `i&&` is for avoiding going out of bounds of the array, but it also means that the first element in the sorted array will not be included. In your example there is no `1` in the resulting array. I.e. `return i&&v!==o[i-1]?v:0;` should be `return v!==o[i-1];` – mflodin Jan 10 '12 at 00:48
5

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

Souvanik Saha
  • 73
  • 1
  • 8
4

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] );
    });
gotofritz
  • 3,341
  • 1
  • 31
  • 47
  • I like this simple solution. If you want the duplicates, though, you need to first find those duplicates, and then make the duplicate list unique. [ 0, 4, 5, 5, 5, 2, 1, 3, 1, 1, 2, 1, 3 ].sort().filter( function(me,i,arr){ return (i!==0) && ( me == arr[i-1] ); }).filter( function(me,i,arr){ return (i===0) || ( me !== arr[i-1] ); }); – Greg Jul 24 '19 at 18:37
4

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);
  }
}
  • Best answer. And if user want duplicate elements array , for this i updated @brandon code var i = codes .length; var duplicate = []; while (i--) { if (codes .indexOf(codes [i]) != i) { if(duplicate.indexOf(codes [i]) === -1){ duplicate.push(arr[i]); } codes.splice(i,1); } } – Himanshu Shekhar Aug 18 '19 at 07:18
4

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;
}
Roysh
  • 1,542
  • 3
  • 16
  • 26
4

With ES6 (or using Babel or Typescipt) you can simply do:

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

https://es6console.com/j58euhbt/

tocqueville
  • 5,270
  • 2
  • 40
  • 54
  • I came to the same syntax, independently, and was just about to add it as a solution when I found this one. It probably isn't the most economical, but it is simple. – nize May 07 '18 at 08:50
4

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);
Muthu Kumaran
  • 17,682
  • 5
  • 47
  • 70
alaahd
  • 274
  • 4
  • 7
4

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]);
guest
  • 706
  • 7
  • 15
4

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);
Divyanshu Rawat
  • 4,421
  • 2
  • 37
  • 53
4

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.

Richie Bendall
  • 7,738
  • 4
  • 38
  • 58
3

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.

vasa
  • 787
  • 8
  • 21
3

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>
Srichakradhar
  • 1,535
  • 1
  • 12
  • 24
  • The code does give back distinct elements, but does not lead to the given result. Please provide complete correct code . – RWC Jan 14 '17 at 11:51
3

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 :

  1. Single line :-P
  2. All inbuilt data structure helping in improving the efficiency
  3. Faster

Description of Logic :

  1. Converting to set to remove all duplicates
  2. Iterating through the set values
  3. 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.

PranavKAndro
  • 921
  • 7
  • 9
3

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;
}
Nosredna
  • 83,000
  • 15
  • 95
  • 122
2
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);
Gajus
  • 69,002
  • 70
  • 275
  • 438
2

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;
  }, []);
}
Rafael Xavier
  • 2,840
  • 27
  • 34
2

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!

Bhushan Shah
  • 1,028
  • 8
  • 20
2

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.

RWC
  • 4,697
  • 2
  • 22
  • 29
2

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);
  1. Test: http://jsperf.com/wgu
  2. Demo: http://jsfiddle.net/46S7g/
  3. 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));
Community
  • 1
  • 1
cocco
  • 16,442
  • 7
  • 62
  • 77
  • 8
    "if you can't read the code above ask, read a javascript book" There's entirely too much code golf in this answer. Naming the variables things like a, b, c makes the code difficult to read. Forgoing curly braces makes it even worse. – River-Claire Williamson Dec 11 '15 at 15:41
  • Most of my answers are based on performance and space savings (other solutions are already posted)... if you don't like it downvote ... else learn javascript, read a js book... or use jquery... they have alot more answers if you search a simple solution. If you really want to learn something i'm happy to explain the code letter per letter. As i can't see a real question in your comment i guess you are just searching for a motive to downvote my answer .... go on... i have no problem with that. Ask a real question or tell me something that does not work with my code. – cocco Dec 11 '15 at 16:59
  • 10
    There is nothing technically wrong with your code. That said, naming variables a, b, c, d, etc. and chaining while loops makes the code difficult to read. So, the code fails to teach anything. – River-Claire Williamson Dec 15 '15 at 22:54
  • Correct me if I'm wrong... But I'm pretty sure the JavaScript interpreter doesn't keep variable names as is. Minifying code like this shouldn't have any effect on the **efficiency** of the code. Yes, it'll result in a smaller amount of **bandwidth** being consumed, but including curly braces, or `reallyLongVariableNamesJustForTheSakeOfIt` shouldn't effect performance at all... Also, making statements like, `read a javascript book` just comes across as obnoxious and, quite frankly, rude. – Jack_Hu Jun 05 '22 at 17:33
2

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)
Prashant Agrawal
  • 660
  • 9
  • 24
2
function GetDuplicates(arr) {
    var i = 0, m = [];
    return arr.filter(function (n) {
        return !m[n] * ~arr.indexOf(n, m[n] = ++i);
    });
}
Raul Rivero
  • 248
  • 3
  • 7
2

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]);
    }
 }
mjwrazor
  • 1,866
  • 2
  • 26
  • 42
2

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

2

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

Gras Double
  • 15,901
  • 8
  • 56
  • 54
2

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']
yoyoyojoe
  • 103
  • 2
  • 8
1

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;
}, []);
1

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.

Gras Double
  • 15,901
  • 8
  • 56
  • 54
1

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))
user2167582
  • 5,986
  • 13
  • 64
  • 121
1

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.

Jeremy A. West
  • 2,162
  • 4
  • 27
  • 40
1

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] */
Debajit Majumder
  • 824
  • 1
  • 11
  • 22
1

//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]
Afanasii Kurakin
  • 3,330
  • 2
  • 24
  • 26
  • 3
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – 31piy Jun 08 '18 at 08:15
  • @whossname may be we should add snippet with more self-explanatory variable names – Afanasii Kurakin Oct 18 '18 at 02:18
  • @AfanasiiKurakin I think it would also help to use an if statement instead of the ternary operator – whossname Oct 18 '18 at 03:09
1
  1. 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)
sg28
  • 1,363
  • 9
  • 19
1

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 );
GAURAV
  • 647
  • 6
  • 18
1

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]));
NAVIN
  • 3,193
  • 4
  • 19
  • 32
1

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) );
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
1

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);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Can we get duplicate values as well as their occurrence like `[2, 2, 3, 3, 4, 4, 4]` ? – hoangfin Nov 30 '18 at 19:28
  • @meteorzeroo that is a different question, but in this case you need in advance to know if the count is is greater than one. that means, you need at lest two pass. – Nina Scholz Nov 30 '18 at 19:34
1

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);
Krishnadas PC
  • 5,981
  • 2
  • 53
  • 54
1

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']
TimNode
  • 733
  • 6
  • 7
1

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); 
Cihat Şaman
  • 3,674
  • 4
  • 14
  • 24
1

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([]));
jjjzi
  • 199
  • 6
1

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
  • This doesn't fully answer the original question but it was very helpful. I [expanded on your method](https://jsfiddle.net/d173yLjs/1/) to achieve the desired result. – Besworks May 28 '22 at 21:42
1

this is the simplest way to find the duplicated elements with ES6 syntax

const arr =[1,2,3,3,21,3, 21,34]
const duplicates = Array.from(new Set(arr.filter((eg, i, ar)=> i !==ar.indexOf(eg))))
console.log(duplicates)
jsBug
  • 348
  • 1
  • 9
1

I have tried this, you will get elements that are unique and element that are repeating in two different arrays.

Complexity O(n)

let start = [1,1,2,1,3,4,5,6,5,5];
start.sort();
const unique=[];
const repeat = [];
let ii=-1 ; 
for(let i =0 ; i<start.length; i++){
    if(start[i]===start[i-1]){
      if(repeat[ii]!==start[i-1]){
        repeat.push(start[i-1]);
        ii++;
      }
    }
    else {
        if(i+1<start.length){
          if(start[i]!==start[i+1]){
            unique.push(start[i]);
          }
        }
        else if(i===start.length-1){
          unique.push(start[i]);
        }
    }
}
console.log(unique) ; 
console.log(repeat);
Brisvera
  • 73
  • 8
1

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.

SplittingField
  • 731
  • 5
  • 11
  • Agreed. The only reason to try different approaches here is that the speed depends on how well various things are implemented in the runtime. And that's going to vary browser-to-browser. For short lists, it probably doesn't matter much how you solve the problem. For large arrays, it does. – Nosredna May 09 '09 at 00:34
0

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

Lucky
  • 16,787
  • 19
  • 117
  • 151
0

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);
rongsir
  • 19
  • 3
0

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,]));
korCZis
  • 550
  • 7
  • 18
  • I didn't down vote but question is to find all duplicates not remove them. Also your answer wont work even in simple examples. – NAVIN Nov 24 '18 at 12:05
0

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);
Saeed Abbaspour
  • 329
  • 4
  • 16
  • This does not do what the question asks. The loop runs one step too far. This answer is substantially the same as other older answers. – Scott Saunders Dec 08 '14 at 17:06
0

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
A.T.
  • 24,694
  • 8
  • 47
  • 65
0

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);
})
  • `const duplicates = Array.from(new Set(arr.filter((eg, i, ar)=> i !==ar.indexOf(eg))))` updated code with single line try it out – jsBug Jun 15 '22 at 12:08
0

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);
}
Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215
0
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;
        }
      }
Elnaz
  • 2,854
  • 3
  • 29
  • 41
0

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);
Bryan
  • 17,201
  • 24
  • 97
  • 123
0

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);
Ziya Vakhobov
  • 465
  • 4
  • 10
  • You should add number of occurences, number of duplicates. – Ziya Vakhobov Apr 19 '18 at 10:46
  • question is to get duplicates, please answer that only. Also your complexity is O(n^2 * result.length). It's such a worst complexity for simple task. – NAVIN Nov 24 '18 at 11:55
0

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).

Andi Domi
  • 731
  • 2
  • 19
  • 48
0

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_;
}
lukeaus
  • 11,465
  • 7
  • 50
  • 60
0

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.

iampz
  • 41
  • 1
  • 7
0

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]));
demo
  • 6,038
  • 19
  • 75
  • 149
Rohit Parte
  • 3,365
  • 26
  • 26
0

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]))
0

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>
Thiago Lagden
  • 161
  • 1
  • 4
0

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);
arjun sah
  • 407
  • 3
  • 11
0
[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;
}, [])
bula
  • 8,719
  • 5
  • 27
  • 44
0
const a = ['a', 'b', 'b']

function findDuplicates(a) {
    return Object.keys(_.pickBy(_.countBy(a), x => x > 1))
}

https://runkit.com/nitzanav/62659f09bd2d5f0008ef46d4

naviram
  • 1,445
  • 1
  • 15
  • 26
0

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;
  }
})
mo3n
  • 1,522
  • 2
  • 10
  • 34
0

I just need to find what the duplicated values are - I don't actually need their indexes or how many times they are duplicated.

A fun and simple task with many hard-to-read answers...

Typescript

function getDuplicatedItems<T>(someArray: T[]): T[] {
    // create a set to iterate through (we only need to check each value once)
    const itemSet = new Set<T>(someArray);

    // from that Set, we check if any of the items are duplicated in someArray
    const duplicatedItems = [...itemSet].filter(
        (item) => someArray.indexOf(item) !== someArray.lastIndexOf(item)
    );

    return duplicatedItems;
}

JavaScript

function getDuplicatedItems(someArray) {
    // check for misuse if desired
    // if (!Array.isArray(someArray)) {
    //     throw new TypeError(`getDuplicatedItems requires an Array type, received ${typeof someArray} type.`);
    // }
    const itemSet = new Set(someArray);
    const duplicatedItems = [...itemSet].filter(
        (item) => someArray.indexOf(item) !== someArray.lastIndexOf(item)
    );
    return duplicatedItems;
}
Warren Halderman
  • 330
  • 1
  • 3
  • 12
0

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);
  1. 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.

  2. 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.

  3. Using for(var key in hash){ if (hash.hasOwnProperty(key)){ alert(key); } } will alert each unique value.

vol7ron
  • 40,809
  • 21
  • 119
  • 172
0

Here Is the answer.

const array = [1, 2, 1, 3, 4, 3, 5];
const results = array.filter((value, index, arr) => arr.indexOf(value) != index);
console.log(results)
//[ 1, 3 ]
Md.Jewel Mia
  • 3,345
  • 3
  • 19
  • 24
0

The Prototype library has a uniq function, which returns the array without the dupes. That's only half of the work though.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
0

/* 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;
}
kennebec
  • 102,654
  • 32
  • 106
  • 127
-1

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>
Thalaivar
  • 23,282
  • 5
  • 60
  • 71
-1
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);
}

});
santhosh
  • 53
  • 1
  • 2
  • 11
  • Although this code might solve the problem, one should always consider adding an explanation to it. – BDL Jan 20 '17 at 14:00
  • Iterating array and storing the unique values in array M and duplicate values in n – santhosh Jan 21 '17 at 14:18
-1

You can proceed by comparing index:

function getDuplicate(array) {
    return array.filter((value, index) => array.value !== index)
}
Dorian B
  • 108
  • 10
-1

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);

https://jsfiddle.net/97Lxupnz/

Shaikh Arbaaz
  • 155
  • 1
  • 4
-1

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;
};
hmabesa
  • 54
  • 3
-1
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)
 } );

enter image description here

Gajender Singh
  • 1,285
  • 14
  • 13
-1
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));
Qiniso
  • 2,587
  • 1
  • 24
  • 30
Mainak Ray
  • 69
  • 1
  • 6
-1

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

Ali CAKIL
  • 383
  • 5
  • 21
-1
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;
}
nickb
  • 59,313
  • 13
  • 108
  • 143
  • -1: Doesn't answer the question asked, which was to identify dupes, not remove them. You should probably also include some text describing your solution. – The DIMM Reaper Aug 20 '15 at 15:37
-3

In this post was useful for duplication check if u are using Jquery.

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!         }     });
Community
  • 1
  • 1
Jothi
  • 14,720
  • 22
  • 68
  • 93
-3
//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);
            }
        }
    }
}
J0e3gan
  • 8,740
  • 10
  • 53
  • 80