Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete duplicate elements from an array [duplicate]

For example, I have an array like this;

var arr = [1, 2, 2, 3, 4, 5, 5, 5, 6, 7, 7, 8, 9, 10, 10] 

My purpose is to discard repeating elements from array and get final array like this;

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

How can this be achieved in JavaScript?

NOTE: array is not sorted, values can be arbitrary order.

like image 282
Mehmet Ince Avatar asked May 25 '13 08:05

Mehmet Ince


1 Answers

It's easier using Array.filter:

var unique = arr.filter(function(elem, index, self) {     return index === self.indexOf(elem); }) 
like image 126
Niccolò Campolungo Avatar answered Sep 20 '22 17:09

Niccolò Campolungo