Created
August 3, 2016 21:52
-
-
Save andrew310/2d49cae2b6e13b0135d014e304c9148f to your computer and use it in GitHub Desktop.
Deduping Arrays in Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var list = [1, 4, 8, 33, 2, 1, 5, 7, 8, 6, 3, 2]; | |
| /*VERSION 1 */ | |
| //retains current order of the list | |
| var dups = {}; //create an object, purpose is we want keys | |
| var uniques = list.filter(function(item){ | |
| return dups.hasOwnProperty(item) ? false : dups[item] = true; | |
| }); | |
| /* How does this work? | |
| / So we are using javascript objects as maps here. As you go through the list, the filter function passes the callback function each item | |
| / in the list to see if it should retain that item in the filtered list. If the item is not in the dups map, we add that key to the dups | |
| / map (dups[item] = true) and then return true so that this item will be retained in the list. | |
| / if the key is found in the dups map, the callback function returns false, and this current item is tossed from the list. | |
| */ | |
| /*VERSION 2*/ | |
| //does NOT retain current order. This is basically the same as the ES6 "set" function, also in Python. | |
| var sortedUniques = list.sort(function(a, b){return a-b;}).filter(function(item, pos, arr){ | |
| return !pos || item != arr[pos - 1]; | |
| }) | |
| /* How does it work? | |
| / the filter function basically checks if the current item is the same as the one before it. (This works because it is sorted). | |
| / if it is the same, it is tossed from the list. If not, it returns true because of the or statement, so it is retained. | |
| /* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment