2538

I have a very simple JavaScript array that may or may not contain duplicates.

var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];

I need to remove the duplicates and put the unique values in a new array.

I could point to all the code that I've tried but I think it's useless because they don't work. I accept jQuery solutions too.

Similar question:

1

51 Answers 51

1
2
3

This was just another solution but different than the rest.

function diffArray(arr1, arr2) {
  var newArr = arr1.concat(arr2);
  newArr.sort();
  var finalArr = [];
  for(var i = 0;i<newArr.length;i++) {
   if(!(newArr[i] === newArr[i+1] || newArr[i] === newArr[i-1])) {
     finalArr.push(newArr[i]);
   } 
  }
  return finalArr;
}
Sign up to request clarification or add additional context in comments.

Comments

2

The simplest way to remove a duplicate is to do a for loop and compare the elements that are not the same and push them into the new array

 var array = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];

 var removeDublicate = function(arr){
 var result = []
 var sort_arr = arr.sort() //=> optional
 for (var i = 0; i < arr.length; i++) {
        if(arr[ i + 1] !== arr[i] ){
            result.push(arr[i])
        }
 };
  return result
}  
console.log(removeDublicate(array))
==>  ["Adam", "Carl", "Jenny", "Matt", "Mike", "Nancy"]

Comments

2

For anyone looking to flatten arrays with duplicate elements into one unique array:

function flattenUniq(arrays) {
  var args = Array.prototype.slice.call(arguments);

  var array = [].concat.apply([], args)

  var result = array.reduce(function(prev, curr){
    if (prev.indexOf(curr) < 0) prev.push(curr);
    return prev;
  },[]);

  return result;
}

1 Comment

What is the purpose for the empty array that you added after the reduce method?
1

I know Im a little late, but here is another option using jinqJs

See Fiddle

var result = jinqJs().from(["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"]).distinct().select();

Comments

1
function arrayDuplicateRemove(arr){
    var c = 0;
    var tempArray = [];
    console.log(arr);
    arr.sort();
    console.log(arr);
    for (var i = arr.length - 1; i >= 0; i--) {
        if(arr[i] != tempArray[c-1]){
            tempArray.push(arr[i])
            c++;
        }
    };
    console.log(tempArray);
    tempArray.sort();
    console.log(tempArray);
}

Comments

1

This is probably one of the fastest way to remove permanently the duplicates from an array

It is 10 times faster than the most functions here. And 78 times 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)
}
  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 explanations about shorter code. How to 'minify' Javascript code

6 Comments

I'd guess its due to posting minified code as a solution...
You would deserve a downvote for claiming to be "fastest" when using while within while and splice (which is a loop too). The fastest way uses a hash-map (JS-Object) and a single loop and is O(n). Try to write the function only using hasOwnProperty(), push() and forEach() and it will be fast. See: en.wikipedia.org/wiki/Big_O_notation
From tests i made and read on many other sites ... direct setting is faster than push (x[0] vs x.push()), forEach is much slower then while & hasOwnProperty() is useless if you don't mess up your array. but i would be happy if you post a code which is faster . You can also downvote. anyway i wrote the function in many different ways , and even if i find it also strange that this one works, this one gave the best results in most browsers on normal arrays.
by normal arrays i mean in the range of 10-100000 elements. Note... again i would be really happy to find a better way as i'm using this function and i'm also someone who tries to avoi useless loops. check out my other functions.
At the other side by fastest i mean fastest code based on the one listed in this specific post.. copy and past those codes and try it yourself... none of the codes here is faster than this strange whilewhile function.
|
0

The easiest way to remove string duplicates is to use associative array and then iterate over the associative array to make the list/array back.

Like below:

var toHash = [];
var toList = [];

// add from ur data list to hash
$(data.pointsToList).each(function(index, Element) {
    toHash[Element.nameTo]= Element.nameTo;
});

// now convert hash to array
// don't forget the "hasownproperty" else u will get random results 
for (var key in toHash)  {
    if (toHash.hasOwnProperty(key)) { 
      toList.push(toHash[key]);
   }
}

Voila, now duplicates are gone!

Comments

0

This solution uses a new array, and an object map inside the function. All it does is loop through the original array, and adds each integer into the object map.If while looping through the original array it comes across a repeat, the

`if (!unique[int])`

catches this because there is already a key property on the object with the same number. Thus, skipping over that number and not allowing it to be pushed into the new array.

    function removeRepeats(ints) {
      var unique = {}
      var newInts = []

      for (var i = 0; i < ints.length; i++) {
        var int = ints[i]

        if (!unique[int]) {
          unique[int] = 1
          newInts.push(int)
        }
      }
      return newInts
    }

    var example = [100, 100, 100, 100, 500]
    console.log(removeRepeats(example)) // prints [100, 500]

Comments

0

Here are some vanilla JS solutions with a complexity of O(n) (fastest possible for this problem). Modify the hashFunction to distinguish the objects (e.g., 1 and "1") if needed. The first solution avoids hidden loops (common in functions provided by Array).

var dedupe = function(a)
{
    var hash = {}, ret = [];
    var hashFunction = function(v) { return ""+v; };
    var collect = function(h)
    {
        if(hash.hasOwnProperty(hashFunction(h)) == false) // O(1)
        {
            hash[hashFunction(h)]=1;
            ret.push(h); // Should be O(1) for Arrays
            return;
        }
    };

    for(var i=0; i<a.length; i++) // This is a loop: O(n)
        collect(a[i]);
    // OR: a.forEach(collect); // This is a loop: O(n)

    return ret;
}

var dedupe = function(a)
{
    var hash = {};
    var isdupe = function(h)
    {
        if(hash.hasOwnProperty(h) == false) // O(1)
        {
            hash[h] = 1;
            return true;
        }

        return false;
    };

    return a.filter(isdupe); // This is a loop: O(n)
}

Comments

0

Here is a nested loop method for removing duplicates in array and preserving original order of elements.

var array = [1, 3, 2, 1, [5], 2, [4]]; // INPUT

var element = 0;
var decrement = array.length - 1;
while(element < array.length) {
  while(element < decrement) {
    if (array[element] === array[decrement]) {
      array.splice(decrement, 1);
      decrement--;
    } else {
      decrement--;
    }
  }
  decrement = array.length - 1;
  element++;
}

console.log(array);// [1, 3, 2, [5], [4]]

Explanation: The inner loop compares first element of array with all other elements starting with element at the highest index. Decrementing towards the first element, a duplicate is spliced from the array.

When the inner loop is finished, the outer loop increments to the next element for comparison and resets the new length of the array.

Comments

-1

Here is another approach using jQuery,

function uniqueArray(array){
  if ($.isArray(array)){
    var dupes = {}; var len, i;
    for (i=0,len=array.length;i<len;i++){
      var test = array[i].toString();
      if (dupes[test]) { array.splice(i,1); len--; i--; } else { dupes[test] = true; }
    }
  } 
  else {
    if (window.console) console.log('Not passing an array to uniqueArray, returning whatever you sent it - not filtered!');
      return(array);
  }
  return(array);
}

Author: William Skidmore

Comments

-1
function removeDuplicates(inputArray) {
            var outputArray=new Array();

            if(inputArray.length>0){
                jQuery.each(inputArray, function(index, value) {
                    if(jQuery.inArray(value, outputArray) == -1){
                        outputArray.push(value);
                    }
                });
            }           
            return outputArray;
        }

Comments

-1

If you don't want to include a whole library, you can use this one off to add a method that any array can use:

Array.prototype.uniq = function uniq() {
  return this.reduce(function(accum, cur) { 
    if (accum.indexOf(cur) === -1) accum.push(cur); 
    return accum; 
  }, [] );
}

["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"].uniq()

Comments

-1

If you're creating the array yourself, you can save yourself a loop and the extra unique filter by doing the check as you're inserting the data;

var values = [];
$.each(collection, function() {
    var x = $(this).value;
    if (!$.inArray(x, values)) {
        values.push(x);
    }
});

1 Comment

Be careful with using the jQuery inArray method: it returns the index of the element in array, not a boolean value. Check the documentation: jQuery.inArray()
-1
function removeDuplicates (array) {
  var sorted = array.slice().sort()
  var result = []

  sorted.forEach((item, index) => {
    if (sorted[index + 1] !== item) {
      result.push(item)
    }
  })
  return result
}

Comments

-1

aLinks is a simple JavaScript array object. If any element exist before the elements on which the index shows that a duplicate record deleted. I repeat to cancel all duplicates. One passage array cancel more records.

var srt_ = 0;
var pos_ = 0;
do {
    var srt_ = 0;
    for (var i in aLinks) {
        pos_ = aLinks.indexOf(aLinks[i].valueOf(), 0);
        if (pos_ < i) {
            delete aLinks[i];
            srt_++;
        }
    }
} while (srt_ != 0);

Comments

-1
var uniqueCompnies = function(companyArray) {
    var arrayUniqueCompnies = [],
        found, x, y;

    for (x = 0; x < companyArray.length; x++) {
        found = undefined;
        for (y = 0; y < arrayUniqueCompnies.length; y++) {
            if (companyArray[x] === arrayUniqueCompnies[y]) {
                found = true;
                break;
            }
        }

        if ( ! found) {
            arrayUniqueCompnies.push(companyArray[x]);
        }
    }

    return arrayUniqueCompnies;
}

var arr = [
    "Adobe Systems Incorporated",
    "IBX",
    "IBX",
    "BlackRock, Inc.",
    "BlackRock, Inc.",
];

1 Comment

Please format the whole post
-1
var lines = ["Mike", "Matt", "Nancy", "Adam", "Jenny", "Nancy", "Carl"];
var uniqueNames = [];

for(var i = 0; i < lines.length; i++)
{
    if(uniqueNames.indexOf(lines[i]) == -1)
        uniqueNames.push(lines[i]);
}
if(uniqueNames.indexOf(uniqueNames[uniqueNames.length-1])!= -1)

for(var i = 0; i < uniqueNames.length; i++)
{
    document.write(uniqueNames[i]);
      document.write("<br/>");
}

2 Comments

Your code works great. but the code 'uniqueNames.pop()' is removing the last array element for no reason. It makes the 'Carl' not listing from the array.
@Santosh Right, I can't edit this right now due to StackOverflow having too many pending edits ATM. But if someone can at any point of time, please edit out the uniqueNames.pop() line.
-1

Quick and easy using Lodash:

var array = ["12346", "12347", "12348", "12349", "12349"];
console.log(_.uniqWith(array, _.isEqual));

Comments

-2
var duplicates = function(arr){
     var sorted = arr.sort();
   var dup = [];
   for(var i=0; i<sorted.length; i++){
        var rest  = sorted.slice(i+1); //slice the rest of array
       if(rest.indexOf(sorted[i]) > -1){//do indexOf
            if(dup.indexOf(sorted[i]) == -1)    
         dup.push(sorted[i]);//store it in another arr
      }
   }
   console.log(dup);
}

duplicates(["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"]);

Comments

-9

ES2015, 1-liner, which chains well with map, but only works for integers:

[1, 4, 1].sort().filter((current, next) => current !== next)

[1, 4]

4 Comments

That works with anything, but only removes sequential duplicates. e.g. [1,1,2,2,3,3] -> [1,2,3] but [1,2,3,1,2,3] -> [1,2,3,1,2,3]
@Kroltan It's actually not a matter of sequential duplicates, but it's a big issue about understanding what's passed to filter: it's (value, index) not (current, next), so it would work for [1,4,1] but not for [2,4,2]...
@Xenos You're right! I skimmed over it too fast xD
I think the approach is good and can easlily work for arrays of other types too, with a slight modification: ["1", "4", "1"].sort().filter((value, index, array) => value !== array[index + 1])
1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.