Remove duplicates from an array - Node.js Array

Node.js examples for Array:Remove Element

Description

Remove duplicates from an array

Demo Code

/**/*  w  ww.j  av  a  2s.  co m*/
 * Remove duplicates from an array
 */
Array.prototype.unique = function ()
{
    var r = [];

    o: for (var i = 0, n = this.length; i < n; i++)
    {
        for (var x = 0, y = r.length; x < y; x++)
        {
            if (r[x] == this[i])
            {
                continue o;
            }
        }

        r[r.length] = this[i];
    }

    return r;
};

/**
 * Remove an array element specified by index
 * Array Remove - By John Resig (MIT Licensed)
 * http://stackoverflow.com/a/9815010/2296282
 */
Array.prototype.remove = function(from, to) {
    "use strict";
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
};

Related Tutorials