Nodejs Array Get Unique getUnique()

Here you can find the source of getUnique()

Method Source Code

// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');

function solution(A) {
   // write your code in JavaScript (Node.js 4.0.0)

   //return A.getUnique().length;
   //return A.filter( onlyUnique ).length;
   if(A.length < 1){
      return 0;//from  w  w  w.  jav a  2  s.  co m
   }else if(A.length == 1){
      return 1;

   }
   var sorted = A.sort();
   var last = A[A.length-1];
   var dinstinctCounter = 1;
   for (var i = A.length - 2; i >= 0; i--) {
      var elem = A[i];
      if (elem === last){
         continue;
      }else{
         dinstinctCounter++;
      }
      last = elem;
   }
   return dinstinctCounter;
}

Array.prototype.getUnique = 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;
}

function onlyUnique(value, index, self) {
   return self.indexOf(value) === index;
}


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

Related

  1. getUnique()
    Array.prototype.getUnique = function(){
        var o = {}, a = [], i, e;
        for(i = 0; e = this[i]; i++){o[e] = 1};
        for(e in o) {a.push (e)};
        return a;
    
  2. getUnique()
    Array.prototype.getUnique = 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;
    
  3. getUnique()
    Array.prototype.getUnique = 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;
    };
    
  4. getUnique()
    Array.prototype.getUnique = function(){
        var that = this;
        for (var i = 0; i < that.length; i++){
            for (var j = i+1; j < that.length; j++){
                if(that[i] === that[j]){
                    that.splice(j, 1);
                    j--;
        return that;
    };
    
  5. getUnique()
    Array.prototype.getUnique = 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;
    for (var i = 1; i<101; i++) {
      var nodeId = Math.round(getRandom(1,3));
      var jobTime = Math.round(getRandom(1,100)) * 10;
      var resources = [] ;
      resources.push(Math.round(getRandom(0,4)));
      resources.push(Math.round(getRandom(0,4)));
      resources.push(Math.round(getRandom(0,4)));
      console.log(i + ":" + nodeId + ":" + jobTime + ":" + resources.getUnique().join("#") );
    function getRandom(min, max) {
      return Math.random() * (max - min) + min;
    
  6. getUnique()
    Array.prototype.getUnique = function(){
        var u = {}, a = [];
        for(var i = 0, l = this.length; i < l; ++i){
      if(this[i] in u)
                continue;
      a.push(this[i]);
      u[this[i]] = 1;
        return a;
    ...