Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all of the duplicate numbers in an array of numbers [duplicate]

I received this question for practice and the wording confused me, as I see 2 results that it might want.

And either way, I'd like to see both solutions.

For example, if I have an array:

let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

I'm taking this as wanting the final result as either:

let finalResult = [1, 2, 3, 4, 5, 8, 9, 10];

OR:

let finalResult = [1, 9, 10];

The difference between the two being, one just removes any duplicate numbers and leaves the rest and the second just wants any number that isn't a duplicate.

Either way, I'd like to write two functions that does one of each.

This, given by someone else gives my second solution.

let elems = {},

arr2 = arr.filter(function (e) {
   if (elems[e] === undefined) {
       elems[e] = true;
    return true;
  }
  return false;
});
console.log(arr2);

I'm not sure about a function for the first one (remove all duplicates).

like image 616
mph85 Avatar asked Mar 20 '19 07:03

mph85


People also ask

How do you remove duplicate numbers in an array?

We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays. sort(arr) method.

How do you remove all duplicates from an array of objects?

To remove the duplicates from an array of objects: Use the Array. filter() method to filter the array of objects. Only include objects with unique IDs in the new array.

How do you filter duplicates in an array?

Use the filter() method: The filter() method creates a new array of elements that pass the condition we provide. It will include only those elements for which true is returned. We can remove duplicate values from the array by simply adjusting our condition.


2 Answers

Using Set and Array.from()

let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

console.log(Array.from(new Set(arr)));

Alternate using regex

regex explanation here

let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

let res = arr
  .join(',')
  .replace(/(\b,\w+\b)(?=.*\1)/ig, '')
  .split(',')
  .map(Number);

console.log(res);

Alternate using objects

let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

let obj = arr.reduce((acc, val) => Object.assign(acc, {
  [val]: val
}), {});

console.log(Object.values(obj));
like image 92
User863 Avatar answered Sep 30 '22 17:09

User863


Just use a simple array.filter one-liner:

let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
let finalResult = arr.filter((e, i, a) => a.indexOf(e) == i).sort(function(a, b){return a - b});
console.log(finalResult);

You could use another filter statement if you wanted the second result:

let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
let finalResult = arr.filter((e, i, a) => a.filter(f => f == e).length == 1).sort(function(a, b){return a - b});
console.log(finalResult);
like image 36
Jack Bashford Avatar answered Sep 30 '22 15:09

Jack Bashford