Javascript Array uniq()

Description

Javascript Array uniq()


Array.prototype.uniq = function() {
  return this.filter(function(value, index) {
    return this.indexOf(value) == index;
  }.bind(this));//from w w  w  .  ja  v a  2  s . co m
};

Javascript Array uniq()

Array.prototype.uniq = function () {
  return [...new Set(this)];
};

Javascript Array uniq()

Array.prototype.uniq = function() {
  var /*from   ww w .j av a  2 s .c om*/
    hash = {};

  for (var i=0; i < this.length; i++) {
    hash[this[i]] = 1; 
  }

  return Object.keys(hash);

}

Javascript Array uniq()

Array.prototype.uniq = function() {
  var a = [];/*from w  ww.  jav a 2s  .  c  om*/
  for (var i = 0; i < this.length; i++){
    if (a.indexOf(this[i]) == -1){
      a.push(this[i]);
    }
  }
  return a
}

Javascript Array uniq()

/**/* w  w w  .  ja v a2 s.c om*/
 * Uniquify an array
 * @return {Array} Uniquified array
 */
Array.prototype.uniq = function () {
    let self = this;

    return self.filter((elem, pos) => self.indexOf(elem) === pos);
};

Javascript Array uniq()

// Javascript implementation of Ruby's Array#uniq

Array.prototype.uniq = function() {
  var seen = {};//from   w  ww. ja  v a  2 s.  c  o  m
  this.forEach(function(i){
    seen[i] = true;
  })

  return Object.keys(seen);
};

Javascript Array uniq()

Array.prototype.uniq = function(){
 var uniqueness = [];
 for(var i = 0; i < this.length; i++){
  var element = this[i];
  if (this.indexOf(element) === this.lastIndexOf(element) || uniqueness.indexOf(element) === -1){
   uniqueness.push(this[i]);/*from w w  w.j  a v a2  s .c o  m*/
  }
 }
 return uniqueness;
};

Javascript Array uniq()

/**/*from   ww w  .  ja v  a2s . c o  m*/
 * Removes duplicate scalar entries from array
 *
 * @return {Array} Returns a new array without duplicate values
 */
function uniq() {
  return this.reduce((p, c) => {
    if (p.indexOf(c) === -1) {
      p.push(c);
    }
    return p;
  }, []);
}

Array.prototype.uniq = uniq;

export default Array;

Javascript Array uniq()

Array.prototype.uniq = function () {
  var arr = [];/*from  ww  w  .j a v  a  2 s .co m*/
  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) : ''
    }
  });
  return arr;
}
// Array.prototype.uniq = function() {
//   return Array.from(new Set(this));
// }

([false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN]).uniq()

Javascript Array uniq()

function u(arr) {
  if (!arr.length) { return [] }
  if (arr.length === 1) { return [arr[0]] }
  return [arr[0]].concat(u(arr.slice(1).filter(function (v) {
    if (typeof v === 'number' && typeof arr[0] === 'number' && isNaN(arr[0]) && isNaN(v)) {
      return false
    }//from  w w w.  ja va  2 s  . c om
    return (v !== arr[0])
  })))
}

Array.prototype.uniq = function () {
  return u(this)
}

console.log([false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN].uniq())

Javascript Array uniq()

Array.prototype.uniq = function(){
    var ans = [];
    var hasNaN = false;
    for(var i = 0,len = this.length; i < len; i++){
        if(ans.indexOf(this[i]) == -1){
            if(this[i] != this[i]){
                if(!hasNaN){
                    ans.push(this[i]);//from  ww w  .  ja  v a 2 s .c  o m
                    hasNaN = true;
                }
            }else{
                ans.push(this[i]);
            }
        }
    }
    return ans;
}

Javascript Array uniq()

Array.prototype.uniq = function() {
  var u = {}, a = [];

  for (var i = 0, ii = this.length; i < ii; i++) {
    if (u.hasOwnProperty(this[i])) {
      continue;//  ww  w  .ja v a 2s  . c om
    }

    a.push(this[i]);
    u[this[i]] = 1;
  }

  return a;
};

Javascript Array uniq()

Array.prototype.uniq = function () {
  var uniques = [];
  this.forEach(function(element){
    var match = false;

    uniques.forEach(function(uniqElement){
      if (uniqElement === element){
        match = true;// w  ww .  j  a  v  a2 s.  co  m
      }
    });

    if (match === false){ uniques.push(element) }

  });
    // if (uniques.find(element) === undefined)
    // uniques.push(element);
  return uniques;
};
var array = [1,2,3,4,5,5];
var uniqs = array.uniq();

console.log(array);
console.log(uniqs);

Javascript Array uniq()

Array.prototype.uniq = function () {
  const dict = {}/*  ww w.j a  va  2  s  .  c om*/
  return this.filter(letter => {
    if (dict[letter]) return false
    dict[letter] = true
    return true
  })
}

Javascript Array uniq()

Array.prototype.uniq = function() {
  let answer = [];
  this.forEach(el => {//from  www  . java2s.c om
    if (answer.indexOf(el) === -1) {
      answer.push(el);
    }
  });
  return answer;
}

Javascript Array uniq()

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

  for (var i = 0; i < this.length; i++ ) {
    if (result.indexOf(this[i]) === -1 ) {
      result.push(this[i]);//from   w w w.  j  a  va  2 s. c  o m
    }
  }

  return result;
};

Javascript Array uniq()

Array.prototype.uniq = function () {
  result = [];//from ww w. j a v  a2s .co  m
  for (var x = 0; x < this.length; x++) {
    if (result.indexOf(this[x]) === -1)
      result.push(this[x]);
  }

  return result;
};

Javascript Array uniq()

Array.prototype.uniq = function() {
  let res = [];/*  ww  w . ja va2  s. c  o m*/
  for (let i = 0; i < this.length; i++) {
    if (res.indexOf(this[i]) === -1) {
      res.push(this[i]);
    }
  }
  return res;
};

Array.prototype.two_sum = function() {
  let res = [];
  this.bind;
  for (let i = 0; i < this.length; i++) {
    for (let j = i + 1; j < this.length; j++) {
      if (this[i] + this[j] === 0) {
        res.push([i, j]);
      }
    }
  }
  return res;
};

function my_transpose(arr){
  let mat = [];

  for (let i = 0; i < arr.length; i++){
    mat.push([]);
    for (let j = 0; j < arr.length; j++){
      mat[i].push(arr[j][i]);
    }
  }
  console.log(arr);
  return mat;
}

console.log(my_transpose(
  [[0, 1, 2],
    [3, 4, 5],
    [6, 7, 8]]));
// console.log([-1, 0, 2, -2, 1].two_sum());

Javascript Array uniq()

'use strict'//w w  w  .j av a2 s.co m

Array.prototype.uniq = function() {
  let result = [];
  this.forEach (el => {
    if (!result.includes(el)) {
      result.push(el);
    }
  });
  return result;
}

[1, 3, 4, 4, 5, 6].uniq();

Array.prototype.twoSum = function() {
  let result = [];
  for (let i = 0; i < this.length; i++ ) {
    for (let j = i + 1; j < this.length; j++ ) {
      if (this[i] + this[j] === 0) {
        result.push([i, j]);
      }
    }
  }
  return result;
}

[-1, 0, 2, -2, 1].two_sum();

function myTranspose(matrix) {
  let cols = [];

  for (let i = 0; i < matrix.length; i ++) {
    cols.push([]);
  }

  matrix.forEach((row, i) => {
    row.forEach((el, j) => {
      cols[j][i] = el;
    });
  });
  return cols;
}

myTranspose([
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8]
  ]);

Javascript Array uniq()

Array.prototype.uniq = function() {
  let uniqArr = [];
  this.forEach( (el) => {/*from w ww  . ja  v  a2  s .  c o  m*/
    if (uniqArr.indexOf(el) === -1) {
      uniqArr.push(el);
    }
  });
  return uniqArr;
};


Array.prototype.twoSum = function() {
  let twoSumArr = [];

  for(let i = 0; i < this.length - 1; i++) {
    for(let j = i + 1; j < this.length; j++) {
      if (this[i] + this[j] === 0) {
        twoSumArr.push([i, j]);
      }
    }
  }
  return twoSumArr;
};

function transpose(mat) {
  let trans = [];
  for(let i = 0; i < mat.length; i++) {
    trans.push([]);
  }
  for(let i = 0; i < mat.length; i++) {
    for(let j = 0; j < mat.length; j++) {
      trans[i][j] = mat[j][i];
    }
  }
  return trans;
}

console.log([1,-1,2,0,-2].twoSum());

Javascript Array uniq()

Array.prototype.uniq = function () {
  var uniques = [this[0]];
  for (var i = 1; i < this.length; i++) {
      if (uniques.indexOf(this[i]) === -1) {
        uniques.push(this[i]);//w  ww .  ja v  a 2  s. c  o m
      }
    }
  return uniques;
}

Javascript Array uniq()

"use strict";//from  w w w .  ja  va  2  s.  c om

Array.prototype.uniq = function() {
  const arr = this.filter( function( item, index, inputArray ) {
             return inputArray.indexOf(item) == index;
  });
  return arr;
};
console.log([1, 1, 2, 2, 3, 3, 4, 4, 5, 5].uniq());

Javascript Array uniq()

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

  for(var num of this) {
    if(!output.includes(num)) {
      output.push(num);//  www .ja va  2s.c  o m
    }
  };

  return output;
};

Javascript Array uniq()

'use strict';/*from  ww w. j av  a 2  s .c  o m*/
Array.prototype.uniq = function() {
  let existing_nums = {};
  let unique_nums = [];
  this.forEach(num => {
    if (!existing_nums[num]) {
      unique_nums.push(num);
      existing_nums[num] = true;
    }
  })
  return unique_nums;
}

Javascript Array 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;//from ww  w .j  av  a2  s  .  c o m
    break;
   }
  }
  if (!found) {
   results.push(this[i]);
  }
 }
 
 return results;
};

// refactored
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;
};

// refactored (requires Enumerable)
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;
 });
};

Javascript Array uniq()

Array.prototype.uniq = function() {  
    var temp = {};

    for(var i=0; i<this.length; i++)  {  
            temp[this[i]] = 1;/*from  w ww.ja  va2s .  c o m*/
    }  

    this.length = 0;
    for(var e in temp) {  
        this.push(e);
    }  
    this.sort()

    return this;  
}

Javascript Array uniq()

Array.prototype.uniq = function () {
  const uniqArr = [];// w  w  w  .j a va 2 s  .com
  for (let i = 0; i < this.length; i++) {
    if (!uniqArr.includes(this[i])) {
      uniqArr.push(this[i]);
    }
  }
  return uniqArr;
};

Javascript Array uniq()

"use strict";// w  ww  . j  a  v  a2  s  .c  om

// dups
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;
};

console.log([1, 1, 2, 2, 3, 3, 4, 4, 5, 5].uniq());



PreviousNext

Related