5

I am trying to return an array with only unique elements that do not have duplicates within the array in no particular order.

[1,2,3,3,3,4,4,2] would return 1

["hello", "truck", 2, "truck", 2, "truck"] would return "hello"

So far I have only been able to return unique elements using the filter() function, but I am not sure where to go.

Basically if there are duplicates I want both values removed from the array.

This sounds simple enough, but I am having a serious mental hiccup.

Below is my code:

function diff(arr1, arr2) {
  var newArr = [];

   newArr = arr1.concat(arr2);

   newArr = newArr.filter(function(elem, index, self) {
        return index == self.indexOf(elem);
    });

    console.log(newArr);
    return newArr;

}

diff([1, 2, 3, 5], [1, 2, 3, 4, 5]);
//should return 4
brndng
  • 103
  • 1
  • 7
  • Possible duplicate of [Remove Duplicates from JavaScript Array](http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array) – Teemu Dec 28 '15 at 18:22
  • 6
    Your function takes 2 arrays, but your question mentions only having one array. which is it? – Idos Dec 28 '15 at 18:23
  • 2
    @Idos I don't mean to be abrasive, but if you look at my code you will notice that I take both arguments and concatenate them into a single array. – brndng Dec 28 '15 at 18:38
  • 1
    Guess I'm a little late, but I'd do the same as most of the answer, but make the function accept any number of arrays, like this -> https://jsfiddle.net/adeneo/71fy3ubn/ – adeneo Dec 28 '15 at 18:50
  • @adeneo This is extremely elegant. I will be going over your fiddle to better understand the logic behind it. – brndng Dec 28 '15 at 18:58
  • @Why does `diff()` take two arguments? The question seems to be about removing duplicates from a single array. – Barmar Oct 13 '16 at 19:44

3 Answers3

6

Compare the indexOf and lastIndexOf, if they are equal then the element has no duplicates. Use .filter with this logic.

function diff(arr1, arr2) {
  return arr1.concat(arr2).filter(function(elem, index, self) {
    return self.indexOf(elem)==self.lastIndexOf(elem);
  });
}

alert(diff([1, 2, 3, 5], [1, 2, 3, 4, 5]));
void
  • 36,090
  • 8
  • 62
  • 107
  • 1
    Hehe, I am so sorry i didn't see your answer. :| OP please mark @LyeFish answer as the right one. – void Dec 28 '15 at 18:42
  • Don't worry about it. Your first answer was along the same line. :) – Lye Fish Dec 28 '15 at 18:42
  • @void Thank you so very much for your quick reply. This is exactly what I was attempting to do. I am still scratching my head with the logic, but I will keep analyzing until I make the connection. Thank you for your contribution as well, LyeFish. – brndng Dec 28 '15 at 18:43
1

You could do something like this:

function findUniques(arr) {
    var i = 0;
    while(i !== arr.length) {
        if(arr.slice(i+1,arr.length-1).indexOf(arr[i]) > -1) {
            arr = arr.splice(i,0);
        } else {
            i++;
        }
    }
}

This would leave the unique items in the array in reverse order of how they were found. To avoid that, you iterate from the end of the array and let i decrease to zero, if you care about the order.

Aaron Gates
  • 469
  • 4
  • 15
-2

To get only the unique items in your array you may use the filter method like so:

function unique(val, index, self) { 
    return index === self.indexOf(val);
}

You can use it like so:

var arr = ['1','2','3','3'];
var uniqueArr = arr.filter(unique); // will return ['1', '2', '3']

This will works without jQuery or prototype.js & for arrays with values of mixed types.

Idos
  • 15,053
  • 14
  • 60
  • 75