Nodejs Array Unique unique()

Here you can find the source of unique()

Method Source Code

Array.prototype.unique = function(){
   var res = [];/*from  w  w  w .j a  v a2s  . com*/
   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));
}

Related

  1. unique()
    Array.prototype.unique = function() {
      this.sort(); 
      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;
    ...
    
  2. 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);
        return a;
    ...
    
  3. unique()
    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;
    };
    ...
    
  4. unique()
    Array.prototype.unique = function() {
        var arr = [];
        for(var i = 0; i < this.length; i++) {
            if(!arr.contains(this[i])) {
                arr.push(this[i]);
        return arr; 
    
  5. 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);
        return a;
    ...
    
  6. unique()
    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;
    ...
    
  7. 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];
    ...
    
  8. 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());
    
  9. 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())