Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts

    Remove duplicates from an array

    Removing all the duplicates from an array in JavaScript doesn't require very much code at all.

    Code:
    Array.prototype.eliminateDuplicates = function() {
      var obj={}, arr = this;
      arr.forEach(function(a,b) {obj[arr[b]]=0;});
      return(Object.keys(obj));
    }
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,448
    Thanks
    11
    Thanked 598 Times in 578 Posts
    Quote Originally Posted by felgall View Post
    Removing all the duplicates from an array in JavaScript doesn't require very much code at all.
    that version can be written a bit faster and simpler as:
    Code:
    Array.prototype.eliminateDuplicates = function() {
      var obj={};
      this.forEach(function(a){this[a]=0;}, obj);
      return Object.keys(obj);
    }
    we don't need to cache the array, we don't need to find out the slot in the array in the function because the forEach callBack's arguments[0] already has the value, we don't need b, and using this inside the forEach turns it into a more-optimized "pure function", increasing the run-time performance and re-usability of the method.

    but, it doesn't handle sub-objects, and is not type-safe, so an array of unique values like ["1",1,[1]].eliminateDuplicates() would return just ["1"].
    more importantly, the compare is done as a string, so any objects tend to get screwed up:[{a:1},{b:2}].eliminateDuplicates() // == ["[object Object]"]


    a type safe version with optimized-performance checks only the thus-found unique value against the current value, in order of most recent (to optimize for sorted arrays) instead of the whole input array:
    Code:
    Array.prototype.eliminateDuplicates = function() {
    	for (var v, r = [], e = this.length, i = 0; i < e; i++){  
            	if(r.lastIndexOf(v=this[i])===-1) r.push( v ); 
    	}
    	return r;
    };
    
    ["1",1,[1]].eliminateDuplicates(); // == [ "1",  1, [1] ]
    a handy non-util method that's type-safe but not as fast (usually) is to just do it functionally:

    Code:
    ["1",1,[1],  1,2,3].filter(function(a,b,c){return b===c.indexOf(a);}) // == [ "1", 1, [1], 2, 3 ]
    Last edited by rnd me; 03-03-2014 at 01:23 AM.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    When I tried using this inside the inner function it failed - I didn't think of making it point to the object rather than the array.

    I agree that your solution is better than mine.
    Last edited by felgall; 03-03-2014 at 02:18 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •