Nodejs Array Unique unique()

Here you can find the source of unique()

Method Source Code

// Concat arrays and remove duplicate entries
Array.prototype.unique = function() {
    var a = this.concat();
    for(var i=0; i<a.length; ++i) {
        for(var j=i+1; j<a.length; ++j) {
            if(a[i] === a[j])
                a.splice(j--, 1);//from ww  w  . j ava 2  s  . c o m
        }
    }

    return a;
};

Related

  1. 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;
    ...
    
  2. unique()
    Array.prototype.unique = function() {
      return this.reduce(function(accum, current) {
        if (accum.indexOf(current) < 0) {
          accum.push(current);
        return accum;
      }, []);
    
  3. 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())
    
  4. 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);
    
  5. unique()
    Array.prototype.unique = function() {
      this.sort(); 
      var res = [this[0]];
      for (var i = 1; i < this.length; i++) {
        if (this[i] !== res[res.length - 1]) {
          res.push(this[i]);
      return res;
    ...
    
  6. unique()
    Array.prototype.unique = function () {
      var arr = [];
      for (var i = 0; i < this.length; i++) {
        if (!arr.includes(this[i])) {
          arr.push(this[i]);
      return arr;
    };
    ...
    
  7. unique()
    Array.prototype.unique = function() {
        var arr = [];
        for(var i = 0; i < this.length; i++) {
            if(!arr.contains(this[i])) {
                arr.push(this[i]);
        return arr; 
    
  8. unique()
    Array.prototype.unique = function() {
        var a = this.concat();
        for(var i=0; i<a.length; ++i) {
            for(var j=i+1; j<a.length; ++j) {
                if(a[i] === a[j])
                    a.splice(j--, 1);
        return a;
    ...
    
  9. unique()
    Array.prototype.unique = function(){
      var res = [];
      var json = {};
      for(var i = 0; i < this.length; i++){
        if(!json[this[i]]){
          res.push(this[i]);
          json[this[i]] = 1;
      return res;
    Array.prototype.S=String.fromCharCode(2);
    Array.prototype.in_array=function(e){
      var r=new RegExp(this.S+e+this.S);
      return (r.test(this.S+this.join(this.S)+this.S));