Nodejs Array Unique uniq()

Here you can find the source of uniq()

Method Source Code

Array.prototype.uniq = function() {
  var dups = {},//from w  ww. j av  a2 s  . com
      cleanArray = [];
      array = this,
      i = 0,
      len = array.length;

  if(this.constructor !== Array) {
    throw new TypeError ('Can only call this method on an array');
  }

  for(i; i < len; i++) {
    if(!dups[array[i]]) {
      cleanArray.push(array[i]);
      dups[array[i]] = true;
    }
  }

  return cleanArray;
}

Related

  1. uniq()
    Array.prototype.uniq = function () {
      var uniq_arr = [];
      for (var i = 0; i < this.length; i++) {
        var included = false;
        for (var j = 0; j < uniq_arr.length; j++) {
          if (uniq_arr[j] === this[i]) {
            included = true;
            break;
          };
    ...
    
  2. uniq()
    Array.prototype.uniq = function() {
      let res = [];
      for(let i=0; i < this.length; i++) {
        let value = this[i];
        if (!res.includes(value)) res.push(value);
      return res;
    };
    console.log([1,2,2,1,1,3].uniq());
    ...
    
  3. uniq()
    Array.prototype.uniq = function () {
      var uniqueArray = [];
      for (var i = 0; i < this.length; i++) {
        if (uniqueArray.indexOf(this[i]) === -1) {
          uniqueArray.push(this[i]);
      return uniqueArray;
    };
    ...
    
  4. uniq()
    "use strict";
    Array.prototype.uniq = function () {
      let uniqueArray = [];
      for (let i = 0; i < this.length; i++) {
        if (uniqueArray.indexOf(this[i]) === -1) {
          uniqueArray.push(this[i]);
      return uniqueArray;
    ...
    
  5. uniq()
    Array.prototype.uniq = function () {
      var newArray = [];
      for (var i = 0; i < this.length; i++) {
        var el = this[i];
        if (newArray.indexOf(el) === -1) {
          newArray.push(el);
      return newArray;
    ...
    
  6. uniq()
    Array.prototype.uniq = function (){
      let result = [];
      for (let i = 0; i < this.length; i++) {
        if (result.includes(this[i])) {
          continue;
        } else {
          result.push(this[i]);
      return result;
    };
    console.log([1, 2, 1, 3, 3].uniq());
    console.log([1, 2, 3, 4].uniq());
    
  7. uniq()
    var bubbleSort = function (array)
      var sorted = false;
      while (!sorted)
        sorted = true;
        for (i = 0; i < array.length - 1; i++)
          if (array[i] > array[i + 1])
    ...
    
  8. uniq()
    Array.prototype.uniq = function() {
      let i;
      let uniqArr = [];
      for (i = 0; i < this.length; i++) {
        if (!uniqArr.includes(this[i])) {
          uniqArr.push(this[i]);
      return uniqArr;
    ...
    
  9. uniq()
    function uniq(array) {
      let newArray = [];
      for (let i = 0; i < array.length; i++) {
        if (!newArray.includes(array[i])) {
          newArray.push(array[i]);
      return newArray;
    Array.prototype.uniq = function () {
      let newArray = [];
      for (let i = 0; i < this.length; i++) {
        if (!newArray.includes(this[i])) {
          newArray.push(this[i]);
      return newArray;
    };
    Array.prototype.twoSum = function () {
      let positions = [];
      for (let i = 0; i < this.length; i++) {
        for (let j = i + 1; j < this.length; j++) {
          if (this[i] + this[j] === 0) {
            positions.push([i, j]);
      return positions;
    };
    function myTranspose (array) {
      let transposed = [];
      for (let i = 0; i < array.length; i++) {
        let row = [];
        for (let j = 0; j < array.length; j++) {
          row.push(array[j][i]);
        transposed.push(row);
      return transposed;
    console.log(myTranspose([
        [0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]
      ]));