Nodejs Array Unique uniq()

Here you can find the source of uniq()

Method Source Code

Array.prototype.uniq = function() {
  var uniq_array = [];

  for (var i=0; i<this.length; i++) {
    curr_ele = this[i]//  w w  w  . j  a v  a  2 s .  c o  m
    if (this.lastIndexOf(curr_ele) === i) {
      uniq_array.push(curr_ele);
    }
  }

  return uniq_array;
};

Related

  1. uniq()
    Array.prototype.uniq = function(){
      var uniques = [];
      for(var i = 0; i < this.length; i++){
        if( uniques.indexOf(this[i]) === -1){
          uniques.push(this[i]);
      return uniques;
    };
    ...
    
  2. uniq()
    Array.prototype.uniq = function () {
      var uniques = [];
      this.forEach(function(element){
        var match = false;
        uniques.forEach(function(uniqElement){
          if (uniqElement === element){
            match = true;
        });
    ...
    
  3. uniq()
    Array.prototype.uniq = function () {
      var arr = [];
      var flag = true;
      this.forEach(function (item) {
        if (item != item) {
          flag && arr.indexOf(item) === -1 ? arr.push(item) : '';
          flag = false;
        } else {
          arr.indexOf(item) === -1 ? arr.push(item) : ''
    ...
    
  4. uniq()
    Array.prototype.uniq = function () {
      var uniq = [];
      for (var i = 0; i < this.length; i++) {
        if (uniq.indexOf(this[i]) === -1) {
          uniq.push(this[i]);
      return uniq;
    };
    ...
    
  5. uniq()
    Array.prototype.uniq = function () {
      const uniqArr = [];
      for (let i = 0; i < this.length; i++) {
        if (!uniqArr.includes(this[i])) {
          uniqArr.push(this[i]);
      return uniqArr;
    };
    ...
    
  6. uniq()
    Array.prototype.uniq = function () {
      var sorted = this.sort();
      var uniques = [sorted[0]];
      for(var i = 1; i < sorted.length; i++) {
        if(sorted[i] !== sorted[i-1]) {
          uniques.push(sorted[i]);
        };
      };
      return uniques;
    ...
    
  7. uniq()
    Array.prototype.uniq = function() {
      var results = [];
      for (var i = 0, len = this.length; i < len; i++) {
        var found = false;
        for (var j = 0, len2 = results.length; j < len2; j++) {
          if (this[i] === results[j]) {
            found = true;
            break;
        if (!found) {
          results.push(this[i]);
      return results;
    };
    Array.prototype.uniq = function() {
      var results = [];
      for (var i = 0, len = this.length; i < len; i++) {
        if (results.indexOf(this[i]) < 0) {
          results.push(this[i]);
      return results;
    };
    Array.prototype.uniq = function(sorted) {
      return this.inject([], function(array, value, index) {
        if (0 == index || (sorted ? array.last() != value : !array.include(value))) {
          array.push(value);
        return array;
      });
    };
    
  8. uniq()
    Array.prototype.uniq = function () {
      var occurrenceHash = {};
      var result = [];
      for (var i = 0; i < this.length; i++) {
        if (!occurrenceHash[this[i]]) {
            occurrenceHash[this[i]] = true;
            result.push(this[i]);
      return result;
    
  9. uniq()
    Array.prototype.uniq = function () {
      var results = [];
      for (var i = 0; i < this.length; i++) {
        var includes = false;
        for (var j = 0; j < results.length; j++) {
          if (results[j] === this[i]) {
            includes = true;
        };
    ...