Eliminate all non-unique elements from an Array - Node.js Array

Node.js examples for Array:Remove Element

Description

Eliminate all non-unique elements from an Array

Demo Code


/**/*ww  w .  j a  v a 2s  . c  om*/
 * Eliminate all non-unique elements from an Array
 *
 * @return {Object} unique element
 *
 */
Array.prototype.unique = function() {
    var u = [];
    o:for (var i=0, n=this.length; i<n; i++) {
        for (var x=0, y=u.length; x<y; x++) {
            if (u[x] == this[i]) {
                continue o;
            }
        }
        u[u.length] = this[i];
    }
    return u;
};

Related Tutorials