Nodejs Array Unique unique()

Here you can find the source of unique()

Method Source Code

Array.prototype.unique = function () {
    var arrVal = this;
    var uniqueArr = [];
    for (var i = arrVal.length; i--; ) {
        var val = arrVal[i];
        if ($.inArray(val, uniqueArr) === -1) {
            uniqueArr.unshift(val);//from   ww  w  .  jav  a2  s.  c  om
        }
    }
    return uniqueArr;
}

Related

  1. unique()
    Array.prototype.unique = function() {
      var json = {};
      var res = [];
      for(var i = 0;i < this.length;i++) {
        if(!json[this[i]]) {
          res.push(this[i]);
          json[this[i]] = 1;
      return res;
    var a = [1,2,3,3,2,1,4,4,0,7];
    console.log(a.unique());
    
  2. unique()
    Array.prototype.unique = function(){
        var u = {}, a = [];
        for(var i = 0, l = this.length; i < l; ++i){
            if(u.hasOwnProperty(this[i])) {
                continue;
            a.push(this[i]);
            u[this[i]] = 1;
        return a;
    
  3. unique()
    Array.prototype.unique = function () {
        var exist = {},
            i;
        for (i = 0; i < this.length; i++) {
            if (exist[this[i]]) {
                this.splice(i, 1);
                i -= 1;
            } else {
                exist[this[i]] = true;
    ...
    
  4. unique()
    Array.prototype.unique = function(){
       var u = {}, a = [];
       for(var i = 0, l = this.length; i < l; ++i){
          if(u.hasOwnProperty(this[i])) {
             continue;
          a.push(this[i]);
          u[this[i]] = 1;
       return a;
    
  5. unique()
    Array.prototype.unique = function(){
      var new_array = [];
      var index = 0;
      var value = null;
      for( count=0; count < this.length; count++){
        value = this[count];
        if( new_array.indexOf(value)==-1 ){
          new_array[index++] = value;
      return new_array;
    };
    
  6. unique()
    Array.prototype.unique = function() {
        var u = this.concat().sort();
        for (var i = 1; i < u.length; ) {
            if (u[i-1] === u[i])
                u.splice(i,1);
            else
                i++;
        return u;
    ...
    
  7. unique()
    Array.prototype.unique = function() {
      return this.reduce(function(accum, current) {
        if (accum.indexOf(current) < 0) {
          accum.push(current);
        return accum;
      }, []);
    
  8. unique()
    Array.prototype.unique = function() {
      var res = [this[0]],
        obj = {};
      for (var i = 1; i < this.length; i++) {
        if (!obj[this[i]]) {
          res.push(this[i]);
          obj[this[i]] = 1;
      return res;
    var arr = ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!'];
    console.log(arr.unique())
    
  9. unique()
    Array.prototype.unique = function()
        var tmp = {}, out = [];
        for(var i = 0, n = this.length; i < n; ++i)
            if(!tmp[this[i]]) { tmp[this[i]] = true; out.push(this[i]); }
        return out;
    var a = [1,2,2,7,4,1,'a',0,6,9,'a'];
    var b = a.unique();
    console.log(a);
    console.log(b);