Nodejs Array Unique unique()

Here you can find the source of unique()

Method Source Code

/**/*  ww w.j  a  va  2 s .  c  o m*/
@Name: Array.prototype.unique
@Author: Paul Visco
@Version: 1.1 11/19/07
@Description: Removes duplicate values from an array

@Return: Array Returns an array of unique values from the original array
@Example:
var myArray = [5, 5, 10, 15];
var answer = myArray.unique();
//answer =[5,10,15];
*/
Array.prototype.unique = function(){
   var n=[];
   this.forEach(function(v){if(!n.inArray(v)){n.push(v);}});
   return n;
};

Related

  1. unique()
    Array.prototype.unique = function() {
        var r = new Array();
        o: for (var i = 0, n = this.length; i < n; i++) {
            for (var x = 0, y = r.length; x < y; x++) {
                if (r[x] == this[i]) {
                    return false;
            r[r.length] = this[i];
    ...
    
  2. unique()
    Array.prototype.unique = function(){
      var array = [];
      var obj = {};
      for(var i = 0; i < this.length; i++){
        if(!obj[this[i]]){
          obj[this[i]] = 1;
          array.push(this[i]);
      console.log(array.length);
      return array;
    };
    var array = new Array();
    array = ['1','2','3','4','12','1','123','2','2','12','4'];
    console.log(array.unique());
    
  3. unique()
    Array.prototype.unique = 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 = ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!'];
    console.log(arr.unique())
    
  4. unique()
    Array.prototype.unique = function () {
        var results  = this.sort();
        for ( var i = 1; i < results.length;i++) {
           if (results[i] === results[i-1]) {
               results.splice(i--,1);
        return results;
    
  5. unique()
    Array.prototype.unique = function() {
        return this.reduce(function(accum, current) {
            if (accum.indexOf(current) < 0) {
                accum.push(current);
            return accum;
        }, []);
    function sleep(milliseconds) {
    ...
    
  6. unique()
    var nums =[{name: 1, val: 1},
    {name: 2, val: 1},
    {name: 3, val: 2},
    {name: 4, val: 2}];
    Array.prototype.unique = function() {
        return this.reduce(function(accum, current) {
            if (accum.indexOf(current) < 0) {
                accum.push(current);
            return accum;
        }, []);
    var newArr = [];
    for (var i = 0; i < nums.length; i++) {
      newArr.push(nums[i].val);
    };
    console.log(newArr.unique());
    
  7. unique()
    Array.prototype.unique = function(){
      'use strict';
      var im = {}, uniq = [];
      for (var i=0;i<this.length;i++){
        var type = (this[i]).constructor.name, 
            val = type + (!/num|str|regex|bool/i.test(type) 
                   ? JSON.stringify(this[i]) 
                   : this[i]);
        if (!(val in im)){uniq.push(this[i]);}
    ...
    
  8. unique()
    Array.prototype.unique = function () {
        var r = new Array();
        o:for(var i = 0, n = this.length; i < n; i++)
          for(var x = 0, y = r.length; x < y; x++)
            if(r[x]==this[i])
               continue o;
    ...
    
  9. unique()
    Array.prototype.unique = function() {
      var solution = [];
      for(var i=0; i<this.length; i++) {
        if(solution.indexOf(this[i]) === -1) solution.push(this[i]);
      return solution;
    };