2918

I have an array of numbers that I need to make sure are unique. I found the code snippet below on the Internet, and it works great until the array has a zero in it. I found this other script here on Stack Overflow that looks almost exactly like it, but it doesn't fail.

How can I determine where the prototype script is going wrong?

Array.prototype.getUnique = function() {
  var o = {}, a = [], i, e;
  for (i = 0; e = this[i]; i++) {o[e] = 1};
  for (e in o) {a.push (e)};
  return a;
}
4
  • For future readers, when start finding that you have to algorithmically modify the contents of your data structure all the time, (order them, remove repeating elements, etc.) or search for elements inside it at every iteration, it's safe to assume that you're using the wrong data structure in the first place and start using one that is more appropriate for the task at hand (in this case a hash set instead of array). Commented Dec 30, 2014 at 11:16
  • Just wanted to point out, a lot of people have suggested using JavaScript Set as a solution, proceed with caution because it is not supported in Internet Explorer. If you have to support IE, then use a polyfill. Commented Nov 18, 2019 at 22:16
  • For those who want to return an array of objects with all properties unique by key: stackoverflow.com/questions/15125920/… Commented Oct 7, 2020 at 11:37
  • Related: Showing unique characters in a string only once. Commented Jan 18, 2023 at 12:04

94 Answers 94

1 2 3
4
-1

Short and sweet prototype solution:

Array.prototype.unique = function() {
  return [...new Set(this)]
}
Sign up to request clarification or add additional context in comments.

Comments

-1

let arr = [1,5,1,2,2,1,3,2,3,3,3,1,10]

for(let i = 0; i<arr.length; i++) {

for(let j = i+1; j<arr.length;j++) {

    if(arr[i] === arr[j]) {
        arr[j] = 0;
    }
    
}
if(arr[i] === 0) {
   
  arr.splice(i,1)
  i--
  

}

}

console.log(arr)

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
Hello, please don't post code only and add an explantation as to why you think that this is the optimal solution. People are supposed to learn from your answer, which might not occur if they just copy paste code without knowing why it should be used.
-2

(function() {
    "use strict";

    Array.prototype.unique = function unique() {
        var self = this;
        return self.filter(function(a) {
            var that = this;
            // console.log(that);
            return !that[a] ? that[a] = true : false;
        }, {});
    }

    var sampleArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    var distinctArray = sampleArray.unique();
    console.log(distinctArray);
})();
Here is the simple way to solve this problem...

Comments

-3

I would sort the array, then all duplicates are neighbours. Then walk once through the array and eliminate all duplicates.

function getUniques(array) {
  var l = array.length
  if(l > 1) {
    // get a cloned copy and sort it
    array = [...array].sort();
    var i = 1, j = 0;
    while(i < l) {
      if(array[i] != array[j]) {
        array[++j] = array[i];
      }
      i++;
    }
    array.length = j + 1;
  }
  return array;
}

Comments

1 2 3
4

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.