Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make an array with unique elements (i.e. remove duplicates)?

I have this code:

var ar = [10,7,8,3,4,7,6];  function isin(n,a){   for (var i=0;i<a.length;i++){     if (a[i]== n) {       var b = true;       return b;     } else {       var c = false;       return c;    }   } }  function unique(a){   var arr = [];   for (var i=0;i<a.length;i++){     if (!isin(a[i],arr)){       arr.push(a[i]);     }   }   return arr; }  alert(unique(ar)); 

In this code, I try to create new unique array (without duplicates) out of the original one. But I still get the original array! Where's my mistake?

like image 656
DrStrangeLove Avatar asked Aug 04 '11 10:08

DrStrangeLove


People also ask

How do you remove duplicates from an array of arrays?

To remove duplicates from an array: First, convert an array of duplicates to a Set . The new Set will implicitly remove duplicate elements. Then, convert the set back to an array.

How do you get all unique values remove duplicates in a JavaScript array?

Answer: Use the indexOf() Method You can use the indexOf() method in conjugation with the push() remove the duplicate values from an array or get all unique values from an array in JavaScript.

How to remove duplicate elements from an unsorted array?

You need to remove the duplicate elements from the array and print the array with unique elements. Thus, the output is 23 35 56 67 54 76. Thus, the output is 5 6 1 7 8 2. You can remove duplicate elements from an unsorted array by following the approach below: Initialize a hash map that'll store all the unique elements of the array.

How do I remove duplicates from an array in Python?

You can remove duplicate elements from an unsorted array by following the approach below: Initialize a hash map that'll store all the unique elements of the array. Traverse the array. Check if the element is present in the array. If the element is present in the array, keep traversing.

How to print an array with unique elements?

You need to remove the duplicate elements from the array and print the array with unique elements. Thus, the output is 1 2 4 6 8 9. Thus, the output is 1 2 3 4 5.

How do I prevent duplicates in an array?

I split all answers to 4 possible solutions: Use object { }to prevent duplicates Use helper array [ ] Use filter + indexOf Bonus! ES6 Setsmethod. Here's sample codes found in answers: Use object { }to prevent duplicates


2 Answers

Or for those looking for a one-liner (simple and functional):

var a = ["1", "1", "2", "3", "3", "1"]; var unique = a.filter(function(item, i, ar){ return ar.indexOf(item) === i; }); 
like image 109
Josh Mc Avatar answered Oct 02 '22 09:10

Josh Mc


Using a plain array and returning the keys of associative array (containing only the "unique" values from given array) is more efficient:

function ArrNoDupe(a) {      var temp = {};      for (var i = 0; i < a.length; i++)          temp[a[i]] = true;      var r = [];      for (var k in temp)          r.push(k);      return r;  }    $(document).ready(function() {      var arr = [10, 7, 8, 3, 4, 3, 7, 6];      var noDupes = ArrNoDupe(arr);      $("#before").html("Before: " + arr.join(", "));      $("#after").html("After: " + noDupes.join(", "));  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div id="before"></div>  <div id="after"></div>

Note: The function does not preserve the order of the items, so if this is important use different logic.

As of IE9 and on all other modern browsers (e.g. Chrome, Firefox) this can become even more efficient by using the Object.keys() method:

function ArrNoDupe(a) {      var temp = {};      for (var i = 0; i < a.length; i++)          temp[a[i]] = true;      return Object.keys(temp);  }    $(document).ready(function() {      var arr = [10, 7, 8, 3, 4, 3, 7, 6];      var noDupes = ArrNoDupe(arr);      $("#before").html("Before: " + arr.join(", "));      $("#after").html("After: " + noDupes.join(", "));  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div id="before"></div>  <div id="after"></div>

Thanks wateriswet for bringing this to my attention. :)

like image 25
Shadow Wizard Hates Omicron Avatar answered Oct 02 '22 10:10

Shadow Wizard Hates Omicron