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?
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.
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.
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.
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.
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.
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
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; });
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. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With