Javascript Array unique()

Description

Javascript Array unique()


// Return new array with duplicate values removed
Array.prototype.unique = function() {
  var a = [];//from   w w w .ja  va 2 s .c  o  m
  var l = this.length;
  for ( var i = 0; i < l; i++ ) {
    for ( var j = i + 1; j < l; j++ ) {
      // If this[i] is found later in the array
      if ( this[i] === this[j] ) j = ++i;
    }
    a.push(this[i]);
  }
  return a;
};

Javascript Array unique()

Array.prototype.unique = function(){
  let n = {}, r = []
  for(let i = 0, l = this.length; i < l; i++){
    if (!n[this[i]])
    {/*from  ww  w  .  j  a v a2  s . com*/
      n[this[i]] = true;
      r.push(this[i]);
    }
  }
  return r;
}

Javascript Array unique()

Array.prototype.unique = function() {
    var o = {}, i, l = this.length, r = [];
    for(i=0; i<l;i+=1) o[this[i]] = this[i];
    for(i in o) r.push(o[i]);
    return r;/*from  ww  w .  j  a va2 s.  c  o m*/
};

Javascript Array unique()

/* http://stackoverflow.com/a/21445415 */
Array.prototype.unique = function() {
  var a = [];// w w w. j  a  va  2  s .  c o  m
  for (var i=0, l=this.length; i<l; i++)
    if (a.indexOf(this[i]) === -1)
      a.push(this[i]);
  return a;
}

Javascript Array unique()

Array.prototype.unique = function(){
    var obj = {};
    for (var i = 0; i < this.length; i++) {
        var str = this[i];
        obj[str] = true;//from  ww  w  .  j  a  va 2  s  .  c o  m
    }
    return Object.keys(obj);
};

exports.Array = Array;

Javascript Array unique()

Array.prototype.unique = function () {
  var o = new Object();
  var i, e;// www.ja  v  a  2s  . c o m
  
  for (i = 0; e = this[i]; i++) {
    o[e] = 1
  };
  
  var a = new Array();
  for (e in o) {
    a.push (e)
  };
  
  return a;
}

Javascript Array unique()

Array.prototype.unique = function() {
   return this.sort().reduce( (a,e) => e === a[a.length-1] ? a : (a.push(e), a), [] )
}

Javascript Array unique()

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

Javascript Array unique()

Array.prototype.unique=function(){

    return this.filter(function (x,i,self){

        return self.indexOf(x)===i;
    });/*from  w  w w  .j a  v a  2  s. c o  m*/
}

Javascript Array unique()

// works in es2015
Array.prototype.unique = function (){
  return Array.from(new Set(this));
}

let testArray = [1,2,5,4,2,6,1];

console.log(testArray.unique());//ww w .  j  av  a  2s  .  c  o  m

Javascript Array unique()

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

Javascript Array unique()

Array.prototype.unique = function () {
    var exist = {},
        i;/*ww w.  j a  v a 2s . co  m*/

    for (i = 0; i < this.length; i++) {
        if (exist[this[i]]) {
            this.splice(i, 1);
            i -= 1;
        } else {
            exist[this[i]] = true;
        }
    }
};

Javascript Array unique()

Array.prototype.unique = function()
{
    var tmp = {}, out = [];
    for(var i = 0, n = this.length; i < n; ++i)
    {/*from  w  w  w. j av a 2 s  .  c  o m*/
        if(!tmp[this[i]]) { tmp[this[i]] = true; out.push(this[i]); }
    }
    return out;
}

var a = [1,2,2,7,4,1,'a',0,6,9,'a'];
var b = a.unique();
console.log(a);
console.log(b);

Javascript Array unique()

Array.prototype.unique = function(){
    var uniqueArray = [];
    var obj = {};
    for(val in this){
       obj[val] = "test";
    };/*w  ww  .ja  v a 2  s .c om*/

    for(key in obj){
      uniqueArray.push(key)
    }

    return uniqueArray;
}

[1, 2, 3, 1, 2, 3].unique();

Javascript Array unique()

//Extend array prototype too see if arrays are 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;
            }//from ww  w  . j  a v  a2s. c  om
        }
        r[r.length] = this[i];
    }
    return true;
}

Javascript Array unique()

Array.prototype.unique = function() {
    var a = this.concat();
    for(var i=0; i<a.length; ++i) {
        for(var j=i+1; j<a.length; ++j) {
            if(a[i] === a[j])
                a.splice(j--, 1);/*from  www . j  a v  a  2 s.co  m*/
        }
    }

    return a;
};

Javascript Array unique()

export function unique (arr) {
  let newArr = []
  let i = 0/*from   w  w w  . ja v  a2s .  c om*/
  for (i = 0; i < arr.length; i++) {
    (newArr.indexOf(arr[i]) === -1) && newArr.push(arr[i])
    !newArr.includes(arr[i]) && newArr.push(arr[i])
  }
  return newArr
}

Array.prototype.unique = function() {
  return Array.from(new Set(this));
}

Javascript Array unique()

Array.prototype.unique = function(){
  var res = [];/*from ww  w . j  a  va 2  s  .c  o m*/
  var json = {};
  for(var i = 0; i < this.length; i++){
    if(!json[this[i]]){
      res.push(this[i]);
      json[this[i]] = 1;
    }
  }
  return res;
}
Array.prototype.S=String.fromCharCode(2);
Array.prototype.in_array=function(e)
{
var r=new RegExp(this.S+e+this.S);
return (r.test(this.S+this.join(this.S)+this.S));
}

Javascript Array unique()

Array.prototype.unique = function() {
  var o = {};//from w ww.ja v a  2 s .c o  m
  for (var i=0; i<this.length; i++) {
    o[this[i]] = this[i];
  }
  var arr = [];
  for (var el in o) {
    arr.push(o[el]);
  }
  return arr;
}

Javascript Array unique()

Array.prototype.unique = function() {
    var arr = [];
    for(var i = 0; i < this.length; i++) {
        if(!arr.contains(this[i])) {
            arr.push(this[i]);/*  w w  w.ja  v a  2 s .c o m*/
        }
    }
    return arr;
}

Javascript Array unique()

Array.prototype.unique = function(){
  /* body... *//*from   ww  w .ja  va 2 s.c  o m*/
  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());

Javascript Array unique()

// ( [ 1, 1, 2, 3, 3, 4 ] ).unique() -> [ 1, 2, 3, 4 ]
Array.prototype.unique = function() {
  var r = [];//from w w w . ja  v  a  2s. c  o m
  var i = 0;
  var l = this.length;
  for ( ; i < l; i++ ) {
    if ( r.indexOf( this[ i ] ) == -1 ) {
      r.push( this[ i ] );
    }
  }
  return r;
}

Javascript Array unique()

"use strict";//from w w w . j a va 2 s.  c  o  m

Array.prototype.unique = function Array_unique() {
    var unique = [];
    for (var i = 0; i < this.length; i++) {
        if (unique.indexOf(this[i]) === -1) {
            unique.push(this[i]);
        }
    }
    return unique;
};

Javascript Array unique()

Array.prototype.unique = function(){
  'use strict';/*www .  j a va 2 s.c  o m*/

  this.sort();
  var ret = [this[0]];

  for(var index=0; index < this.length; index++){
    if(this[index] != ret[ret.length - 1]){
      ret.push(this[index]);
    }
  }
  return ret;
};

Javascript Array unique()

Array.prototype.unique = function() {
    var a = this.concat();
    for(var i=0; i<a.length; ++i) {
        for(var j=i+1; j<a.length; ++j) {
            if(a[i] === a[j])
                a.splice(j, 1);//from   w  w  w  .jav  a2s.co  m
        }
    }
    return a;
};

Javascript Array unique()

Array.prototype.unique = function() {
  return this.reduce(function(accum, current) {
    if (accum.indexOf(current) < 0) {
      accum.push(current);/*from   w  w  w. j  av a2 s  . c  om*/
    }
    return accum;
  }, []);
}

Javascript Array unique()

Array.prototype.unique = function(){
 
 var new_array = [];
 var index = 0;/*w  ww. j  a  v  a  2s  .  c  om*/
 var value = null;
 
 for( count=0; count < this.length; count++){
  
  value = this[count];
  
  if( new_array.indexOf(value)==-1 ){
   new_array[index++] = value;
  }
  
 }
 
 return new_array;
 
};

Javascript Array unique()

Array.prototype.unique = function() {
    var u = this.concat().sort();
    for (var i = 1; i < u.length; ) {
        if (u[i-1] === u[i])
            u.splice(i,1);//w w  w.  j  av  a2s.c  om
        else
            i++;
    }
    return u;
}
var uniq = [1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d"].unique();

Javascript Array unique()

Array.prototype.unique = function() {
  var a = [];//w  ww .  j a v a2  s  .c  o  m
  var l = this.length;
  for (var i = 0; i < l; i++) {
    for (var j = i + 1; j < l; j++) {
      if (this[i] === this[j])
        j = ++i;
    }
    a.push(this[i]);
  }
  return a;
};

Javascript Array unique()

Array.prototype.unique = function () {
    var r = [];//  w  ww .  jav  a2  s .co m
    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;
        }
      }
      r[r.length] = this[i];
    }
    return r;
};

Javascript Array unique()

// UNIQUE//from w  w w.j a  va 2  s .  c  o m
// ------

// Adds the unique method ot an Array 
// of objects.  Assumes a 'toString' 
// method exists for each object.

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

 for(var i = 0, l = this.length; i < l; ++i){
  if(u.hasOwnProperty(this[i].toString())) {
   continue;
  }

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

 return a;
};

Javascript Array unique()

Array.prototype.unique = function() {
 this.sort();//from   w ww . j a v  a  2s  . c  o m
 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;
}

var arr = ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!'];
console.log(arr.unique())

Javascript Array unique()

Array.prototype.unique = function() {
 var res = [this[0]],
  obj = {};/*  ww w  .  j  a v a2 s. c om*/

 for (var i = 1; i < this.length; i++) {
  if (!obj[this[i]]) {
   res.push(this[i]);
   obj[this[i]] = 1;
  }
 }
 return res;
}

var arr = ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!'];
console.log(arr.unique())

Javascript Array unique()

//Takes an array of values (string or numbers) and
//returns a new array with only the unique elements in the array.
//Usage://from   w  w w .j av  a2  s. co m
//[1,2,3,4,4,5,5].unique()
//>[1,2,3,4,5]

Array.prototype.unique = function () {
 var arr = [];
 for (var i = 0; i < this.length; i++) {
  if (!arr.includes(this[i])) {
   arr.push(this[i]);
  }
 }
 return arr;
};

Javascript Array unique()

Array.prototype.unique = function(){
    var u = {}, a = [];
    for(var i = 0, l = this.length; i < l; ++i){
        if(u.hasOwnProperty(this[i])) {
            continue;/* ww  w.j  a va2s.c o m*/
        }
        a.push(this[i]);
        u[this[i]] = 1;
    }
    return a;
}

Javascript Array unique()

Array.prototype.unique = function() {
 var json = {};/*from   w w  w.j a  v  a  2 s . c  om*/
 var res = [];
 for(var i = 0;i < this.length;i++) {
  if(!json[this[i]]) {
   res.push(this[i]);
   json[this[i]] = 1;
  }
 }
 return res;
}

var a = [1,2,3,3,2,1,4,4,0,7];
console.log(a.unique());

Javascript Array unique()

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

Javascript Array unique()

var arr=[1,2,2,3,4,5,6,7,7,6,5,4,4,3];

Array.prototype.unique=function(){
 var _this=this;/* w w w . j  ava  2s. c o m*/
 var newArr=[];
 for(var i=0;i<_this.length;i++){
  if(newArr.indexOf(_this[i])===-1){
   newArr.push(_this[i]);
  }
 }
 return newArr;
}
arr.unique();

Javascript Array unique()

Array.prototype.unique = function () {
    var r = new Array();
    o:for(var i = 0, n = this.length; i < n; i++)
    {/*from   w  ww.jav  a 2  s.  c o  m*/
     for(var x = 0, y = r.length; x < y; x++)
     {
      if(r[x]==this[i])
      {
       continue o;
      }
     }
     r[r.length] = this[i];
    }
    return r;
}

Javascript Array unique()

Array.prototype.unique = function() {
  var arr = [];//from w  ww.  j a v  a 2s .  co m
  for(var i = 0; i < this.length; i++) {
    if(arr.indexOf(this[i]) === -1) {
      arr.push(this[i]);
    }
  }
  return arr;
};

Javascript Array unique()

'use strict'//from ww w . jav a 2 s  .c o m


Array.prototype.unique = function() {
    var o = {}, i, l = this.length, r = [];

    for (i = 0; i < l; i += 1) {
        o[this[i]] = this[i];
    }

    for (i in o) {
        r.push(o[i]);
    }
    return r;
};

Javascript Array unique()

const Arr = [1,2,3,2,'l',3,6,10,10]

// ES6/*from w  w w.  java  2  s.  com*/
let newArr = [...new Set(Arr)]
console.log(newArr)


// 2
const newArr2 = Arr.filter((ele, index, self) => {
  return index === self.indexOf(ele)
})
console.log(newArr2)

// 3
Array.prototype._unique = function() {
  let newArr = []
  let oneArr = this
  for (let i = 0; i< oneArr.length; i++) {
    if(newArr.indexOf(oneArr[i]) === -1) {
        newArr.push(oneArr[i])
        console.log(newArr)
    }
  }
  return newArr
}

console.log(Arr._unique())

Javascript Array unique()

Array.prototype.unique = function () {
 return this.filter(function (item, pos, self) {
  return self.indexOf(item) == pos;
 });//from  w ww. j a  va 2 s . c  o  m
};

Javascript Array 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]);
  }/*from   w w w.  j ava  2  s  .c o  m*/
  return solution;
};

Javascript Array unique()

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

Javascript Array unique()

Array.prototype.unique =/*from w  ww.  ja v a 2s.  co  m*/
  function() {
    var a = [];
    var l = this.length;
    for(var i=0; i<l; i++) {
      for(var j=i+1; j<l; j++) {
        // If this[i] is found later in the array
        if (this[i] === this[j])
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
  };

  
  Array.prototype.clean = function(deleteValue) {
   for (var i = 0; i < this.length; i++) {
     if (this[i] == deleteValue) {         
       this.splice(i, 1);
       i--;
     }
   }
   return this;
 };

Javascript Array unique()

Array.prototype.unique = function () {
  var self = this;

  var tmp = new Array();
  for (var i = 0, l = self.length; i < l; i++) {
    if (tmp.indexOf(self[i]) === -1) {
      tmp.push(self[i]);/* w ww.ja  v a  2s  .  com*/
    }
  }
  return tmp;
}

Javascript Array 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;/*  w  w  w .j  av  a2  s.com*/
    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())

Javascript Array unique()

Array.prototype.unique = function() {
 var arr = [this[0]];

 for (var i = 1; i < this.length; i++) {
  if ( arr.indexOf (this[i]) === -1) {
   arr.push(this[i]);/*from w  ww .j  a va2s.c  om*/
  }
 }
 return arr;
}


var arr = [1, 2, 1, 3, 5, 3, 5, '5'];
console.log(arr.unique());

console.time();
var unique2 = function(arr) {
 var 
  ary = [],
  i = 0,
  key;

 for (; key = arr[i++]; ) {
  if ( ary.indexOf(key) === -1 ) {
   ary.push(key);
  }
 }

 return ary;
}

console.log(unique2(arr));
console.timeEnd();

Javascript Array unique()

// This file contains method extensions to the built-in Array class.

/*//  w w w  .j a  va 2 s.co  m
 * Array - unique method.
 * Returns a new array that is the same as the current one but with all of the duplicates removed.
 * NOTE: Does not work on objects because it uses the == operator and not .equals.
 */
Array.prototype.unique = function () {
 var r = [];
 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;
   }
  }
  r[r.length] = this[i];
 }
 return r;
}

Javascript Array unique()

// Remove all duplication in Array
// TODO: Performance enhancement
Array.prototype.unique = function() {
    var a = this.concat();
    for(var i=0; i<a.length; ++i) {
        for(var j=i+1; j<a.length; ++j) {
            if(a[i] === a[j])
                a.splice(j--, 1);//from   ww  w. j  a v  a2  s.com
        }
    }

    return a;
};

Javascript Array unique()

/*/*from   ww w .  j  a  v  a  2 s  . c o  m*/
Getting an array of unique values from an array 
containing duplicates in JavaScript
*/

// solution 1: add method to array prototype
Array.prototype.unique = function() {
 var unique = [];
 for (var i = 0; i < this.length; i++) {
  if (unique.indexOf(this[i]) == -1) {
   unique.push(this[i]);
  }
 }
 return unique;
};

// solution 2: using reduce method
var arrayUnique = function(a) {
 //p: previousValue (fist:initArray)
 //c:currentValue
 return a.reduce(function(p, c) {
  if (p.indexOf(c) < 0){ 
   p.push(c);
  }
  return p;
 }, []); // []: initArray
};

Javascript Array unique()

// Returns an array of all the distinct elements in a given Array

Array.prototype.unique = function() {

    // Prototypes throw TypeErrors when the context or arguments are invalid

    if (Object.prototype.toString.call(this) !== '[object Array]') {
        throw new TypeError("`this` must be Array, not " + typeof this);
    }//from ww w .  j  av  a 2  s  .  c  om

    var unique = [];

    for (var i = 0, l = this.length; i < l; i++) {
        var element = this[i];

        if (unique.indexOf(element) === -1) {
            unique.push(element);
        }
    }

    return unique;
};

Javascript Array unique()

Array.prototype.unique = function() {
    var arr = [];
    for(var i = 0; i < this.length; i++) {
        if(!arr.contains(this[i])) {
            arr.push(this[i]);//from  w  w w.j a va 2  s  . c o m
        }
    }
    return arr; 
};

Javascript Array unique()

Array.prototype.unique = function () {
  var arr = this;
  return this.filter(function (el, idx) {
    return arr.lastIndexOf(el) === idx;
  });//from   w w  w .  jav  a2s .co  m
};

Javascript Array unique()

/**/*www. j  a v a2 s .c o  m*/
 * Extend native objects
 */

/**
 * remove duplicates
 * @return {Array}
 */
Array.prototype.unique = function() {

    var u = {}, a = [];
    for(var i = 0, l = this.length; i < l; ++i){
        if(u.hasOwnProperty(this[i])) {
            continue;
        }
        a.push(this[i]);
        u[this[i]] = 1;
    }
    return a;
};

Javascript Array unique()

Array.prototype.unique = function() {
 return this.filter( function(value, index, self) { 
  return self.indexOf(value) === index;
 } );// w  ww.  j ava  2 s.  co m
};

Javascript Array unique()

Array.prototype.unique = function() {
 var tmp={}, len = this.length;
 for (var i=0; i<len; i++) {
  if (typeof tmp[this[i]] == "undefined") {
   tmp[this[i]] = 1;//  w  w w.  jav  a 2s  .  co  m
  }
 }
 this.length = 0;
 len = 0;
 for (var i in tmp) {
  this[len++] = i;
 }
 return this;
};
var a = [1,7,1,1,7,2,3,'aaa','aaa','bba'];
b = a.unique();
document.write(b);

function arrUnique(arr){
 var tmp = {},len = this.length;
 
}

Javascript Array unique()

Array.prototype.unique = function() {
    var a = [];/*from   ww  w  . j a v a2  s.co m*/
    for (var i=0, l=this.length; i<l; i++)
        if (a.indexOf(this[i]) === -1)
            a.push(this[i]);
    return a;
};

Javascript Array unique()

Array.prototype.unique = function() {
  return this.filter(function(item, idx, array) {
    return array.indexOf(item) === idx;
  });/*from  w  ww  .  j a va 2s.  co  m*/
}

Javascript Array unique()

Array.prototype.unique = function(){
 var r = new Array();
 o:for (var i = 0; i < this.length; i++) {
  for (var x = 0; x < r.length; x++) {
   if (r[x]==this[i]) continue o;
  }/*  ww w . j a  v  a2  s .  c o m*/
  r[r.length] = this[i];
 }
 return r;
};

Javascript Array unique()

var _base;/*  w w w  . j a  v  a  2  s .  com*/

Array.prototype.unique = function() {
    var a = this.concat();
    for(var i=0; i<a.length; ++i) {
        for(var j=i+1; j<a.length; ++j) {
            if(a[i] === a[j])
                a.splice(j--, 1);
        }
    }

    return a;
};

Javascript Array unique()

Array.prototype.unique =/*from   www  .  j av  a2s .  c  o  m*/
  function() {
    var a = [];
    var l = this.length;
    for(var i=0; i<l; i++) {
      for(var j=i+1; j<l; j++) {
        // If this[i] is found later in the array
        if (this[i] === this[j])
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
  };

Javascript Array unique()

Array.prototype.unique = function() {
  const obj = {};//from  w  w w  .  j  ava 2s  . c  om
  const arr = [];

  this.forEach((item) => {
    if (obj[item]) {
      return;
    } else {
      arr.push[item];
      obj[item] = true;
    }
  })

  return arr;
}

Array.prototype.unique = function() {
  const arr = [];

  this.sort().forEach((item) => {
    if (arr[arr.length - 1] !== item) {
      arr.push(item)
    }
  })

  return arr;
}

Array.prototype.unique = function() {
  const arr = [];

  this.forEach((item) => {
    if (arr.indexOf(item) === -1) {
      arr.push(item);
    }
  })

  return arr;
}

Javascript Array unique()

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

Javascript Array unique()

Array.prototype.unique = function unique() {
    return this.reduce(function(a,item){
        var key = item.toLowerCase();
        if (!a[key]) a.push(key);
        return a;
    }, []);/*from   w w w.ja va  2 s.  c o m*/
};

Javascript Array unique()

Array.prototype.unique = function() {
  var uniques = [];
  this.forEach(function(value) {
    if (uniques.indexOf(value) == -1)
      uniques.push(value);//from  ww w  .  j  ava 2  s  .c  om
  });
  return uniques;
};

Javascript Array unique()

Array.prototype.unique = function () {
  var r = new Array();
  o:for(var i = 0, n = this.length; i < n; i++)
  {/*from   w  w  w.j a  v a2 s.  co m*/
    for(var x = 0, y = r.length; x < y; x++)
    {
      if(r[x]==this[i])
      {
        continue o;
      }
    }
    r[r.length] = this[i];
  }
  return r;
}

var unique = arr.unique();

i=0;
for (i;i<unique.length;i++) {
  console.log(unique[i]);
}

Javascript Array unique()

Array.prototype.unique = function() {
  var result = [];
  var hash = {};/*  w  w w  .  j  a  va2s. c  o  m*/
  var len = this.length;
  for ( var i = 0; i < len; i ++ ) {
    if ( !hash.hasOwnProperty( '_' + arr[i] ) ) {
      hash[ '_' + arr[i] ] = 1;
      result.push( this[i] );
    }
  }
  return result;
};

function unique( arr ) {
  var result = [ arr[0] ];
  var len = arr.length;
  for ( var i = 1; i < len; i ++ ) {
    var bool = false;
    for ( var j = 0; j < result.length; j ++ ) {
      if ( arr[i] === result[j] ) {
        bool = true;
        break;
      }
    }
    if ( !bool ) {
      result.push( arr[i] );
    }
  }
}

Javascript Array unique()

// Uses a reverse-unique-algorithm for Google Reader
Array.prototype.unique = function(){
   var u = {}, a = [];
   for(var l = 0, i = this.length-1; i >= 0; --i){
      if(u.hasOwnProperty(this[i])) {
         continue;/*from   w w  w .j a v a  2  s  .co  m*/
      }
      a.unshift(this[i]);
      u[this[i]] = 1;
   }
   return a;
};

Javascript Array unique()

Array.prototype.unique = function() {
 var a = this.concat();
 for(var i=0; i<a.length; ++i) {
  for(var j=i+1; j<a.length; ++j) {
   if(a[i] === a[j])
    a.splice(j--, 1);//  www. j a v a2  s. c  o  m
  }
 }
 return a;
};

function mergeRelativeArray(arr,mergePoint) {
 if (arr.length == 1 || mergePoint == arr.length) {
  return arr;
 }
 var p = mergePoint;
 while (p > 0) {
  var arr1 = arr[mergePoint];
  var arr2 = arr[p - 1];
  var unique = arr2.concat(arr1).unique();
  if (unique.length < (arr1.length + arr2.length)) {
   arr.splice(mergePoint, 1);
   arr[p-1] = unique;
   return mergeRelativeArray(arr, 1);
  } else {
   p--;
  }
 }
 return mergeRelativeArray(arr, mergePoint + 1);
}

module.exports = mergeRelativeArray;

Javascript Array unique()

// Array unique method
Array.prototype.unique = function() {
    var unique = [];
    for (var i = 0; i < this.length; i++) {
        if (unique.indexOf(this[i]) == -1) {
            unique.push(this[i]);/*from   w  ww. j  a va 2 s  .co m*/
        }
    }
    return unique;
};

Javascript Array unique()

// Make merged array unique
Array.prototype.unique = function() {
    var array = this.concat();
    for(var i = 0; i < array.length; ++i) {
        for(var j = i + 1; j < array.length; ++j) {
            if(array[i] === array[j])
                array.splice(j--, 1);/* www  .  j a va2s  . c om*/
        }
    }

    return array;
};

Javascript Array Unique()

//Get keys from array which are true
function array_keys_true(array){
 var result = [];
 for(element in array)
  if(element)/*from   w  w w  . j  a v a 2  s .co m*/
   result.push(array[element]);
 return result;
}

/*Remove value from array
 Example:
 var array = ['one', 'two', 'three'];
 array_remove(array, 'three');
 Result:
 array = ['one', 'two'];
*/
function array_remove(arr) {
 var what, a = arguments, L = a.length, ax;
 while (L > 1 && arr.length) {
  what = a[--L];
  while ((ax= arr.indexOf(what)) !== -1) {
   arr.splice(ax, 1);
  }
 }
 return arr;
}

Array.prototype.Unique = function(){
 var u = {}, a = [];
 for(var i = 0, l = this.length; i < l; ++i){
  if(u.hasOwnProperty(this[i]))
   continue;
  a.push(this[i]);
  u[this[i]] = 1;
 }
 return a;
}

Javascript Array unique()

/**//  w  w  w .  j a v  a 2s .c  o m
 * http://codereview.stackexchange.com/questions/83717
 */
Array.prototype.unique = function() {
  var seen = {}
  return this.filter(function(x) {
    if (seen[x])
      return
    seen[x] = true
    return x
  })
}

/**
 * http://stackoverflow.com/questions/27266550
 */
Array.prototype.flatten = function() {
  var toString = Object.prototype.toString;
  var arrayTypeStr = '[object Array]';

  var result = [];
  var nodes = this.slice();
  var node;

  if (!this.length) {
    return result;
  }

  node = nodes.pop();

  do {
    if (toString.call(node) === arrayTypeStr) {
      nodes.push.apply(nodes, node);
    } else {
      result.push(node);
    }
  } while (nodes.length && (node = nodes.pop()) !== undefined);

  result.reverse();
  return result;
}

Javascript Array unique()

// Takes a single-dimensional array and returns a new array with only unique values kept.
Array.prototype.unique = function() {
  var key, output, value, _ref, _results;
  output = {};//from w  w w. j  av  a 2 s  . c o m
  for (key = 0, _ref = this.length; 0 <= _ref ? key < _ref : key > _ref; 0 <= _ref ? key++ : key--) {
    output[this[key]] = this[key];
  }
  _results = [];
  for (key in output) {
    value = output[key];
    _results.push(value);
  }
  return _results;
};

Javascript Array unique()

/*Helper functions*/
'use strict';/*from  w  ww.j  ava  2 s.c o  m*/
Array.prototype.unique = function() {
  var newArray = [];
  for (var i = 0, l = this.length; i < l; i++) {
    if (newArray.indexOf(this[i]) === -1 && this[i] !== '') {
      newArray.push(this[i]);
    }
  }
  return newArray;
};

Javascript Array unique()

/***/*  w ww  .j a  v a  2  s . c om*/

Common Characters

Write a function that accepts two strings as arguments, and returns only the characters that are common to both strings.

Your function should return the common characters in the same order that they appear in the first argument. 
Do not return duplicate characters and ignore whitespace in your returned string.

Example: commonCharacters('acexivou', 'aegihobu')

Returns: 'aeiou'

***/


Array.prototype.unique = function() {
  var arr = [];
  for(var i = 0; i < this.length; i++) {
    if(arr.indexOf(this[i]) === -1) {
      arr.push(this[i]);
    }
  }
  return arr;
};

commonCharacters = function(string1, string2){
  var solution = '';
  var array1 = string1.replace(/ /g,'').split('').unique();
  var array2 = string2.replace(/ /g,'').split('').unique();
  for(var i=0; i<array1.length; i++) {
    if(array2.indexOf(array1[i]) !== -1) solution += array1[i];
  }
  return solution;
};



PreviousNext

Related