Nodejs Array Unique unique1()

Here you can find the source of unique1()

Method Source Code

Array.prototype.unique1 = function () {
  var a = [],/*from   ww  w .  j  a  v  a2 s . c  om*/
    o = {},
    n = this.length;

    for (var i=0; i<n; ++i) {
      o[this[i]] = this[i];
    }
    for (var i in o) {
      a.push(o[i]);
    }
    return a;
};

Related

  1. unique(fun, map)
    (
      Array.prototype.unique = function (fun, map) {
        fun = fun || (function(c) { return c; });
        var arrayUnique = [this[0]], 
        arrayUniqueAtt = [fun(this[0])];
        this.forEach(function(el){
          if (arrayUniqueAtt.indexOf(fun(el)) === -1) {
            arrayUniqueAtt.push(fun(el));
            arrayUnique.push(el);
    ...
    
  2. unique(k)
    Array.prototype.unique = function(k) {
        var self = this;
        var result = [];
        var sublen = 0;
        for (var i = 0, l = self.length; i < l; i++) {
            var v = self[i];
            if (!k) {
                if (result.indexOf(v) === -1) {
                    result.push(v);
    ...
    
  3. unique(mutate)
    Array.prototype.unique = function (mutate) {
      var unique = this.reduce(function (accum, current) {
        if (accum.indexOf(current) < 0) {
          accum.push(current);
        return accum;
      }, []);
      if (mutate) {
        this.length = 0;
    ...
    
  4. unique(str)
    Array.prototype.unique = function(str){
      const seen = new Map()
      return this.filter((a) => {
        return !seen.has(a['name']) && seen.set(a['name'], 1)
      })
    
  5. unique1()
    Array.prototype.unique1 = function(){
      var res = [this[0]];
      for(var i = 1; i < this.length; i++){
        var repeat = false;
        for(var j = 0; j < res.length; j++){
          if(this[i] == res[j]){
            repeat = true;
            break;
        if(!repeat){
          res.push(this[i]);
      return res;
    var arr = [1, 'a', 'a', 'b', 'd', 'e', 'e', 1, 0]
    console.log(arr.unique1());
    
  6. unique2()
    Array.prototype.unique2 = 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;
    ...
    
  7. unique2()
    Array.prototype.unique2=function () {
        var n={};
        var tmp=[];
        for(var i=0;i<this.length;i++){
            if(!n[this[i]]){
                n[this[i]]=true;
                tmp.push(this[i]);
        return tmp;
    
  8. unique2()
    Array.prototype.unique2 = function () {
      var a = [],
        n = this.length;
        for (var i=0; i<n; ++i) {
          for (var j=i+1; j<n; ++j) {
            if(this[i]===this[j]) j = ++i;
          a.push(this[i]);
      return a;
    };
    
  9. unique2()
    Array.prototype.unique2 = function () {
      var result = [];
      for (var i = 0; i < this.length; i++) {
        if (result.indexOf(this[i]) == -1) {
          result.push(this[i]);
      return result;