Nodejs Array Unique unique()

Here you can find the source of unique()

Method Source Code

//Write a function called sumIntervals that accepts an array of intervals, and returns the sum of all the interval lengths. Overlapping intervals should only be counted once.
// sumIntervals( [
//    [1,4],/*from   w  w w  .  j  av a 2  s  .co m*/
//    [7, 10],
//    [3, 5]
// ] ); //=> returns 7  ( Since [1, 4] and [3, 5] overlap, we can treat the interval as [1, 5], which has a length of 4.)

// first extend arrays to have an "unique" method
Array.prototype.unique = function() {
  var output = {};
  for (var key = 0; key < this.length; key++) {
    output[this[key]] = this[key];
  }
  var results = [];
  for (key in output) {
    var value = output[key];
    results.push(value);
  }
  return results;
};

function sumIntervals(intervals){
  var ranges = intervals.map(function(x) {
    return (function() {
      var results = [];
      for (var i = x[0]; i < x[1]; i++){
        results.push(i); }
      return results;
    }).apply(this);
  });
  var one = ranges.reduce(function(x, y) {
    return x.concat(y);
  });
  return one.unique().length;
};

Related

  1. unique()
    Array.prototype.unique = function() {
        return this.reduce(function(p, c) { 
                        if (p.indexOf(c) < 0) p.push(c);
                        return p;
                    }, []);
    
  2. unique()
    function stringToBoolean(string) {
      switch(string.toLowerCase()) {
        case "true": case "yes": case "1": return true;
        case "false": case "no": case "0": case null: return false;
        default: return string;
    Array.prototype.unique = function() {
        var o = {},
    ...
    
  3. unique()
    Array.prototype.unique = function() {
      var a=[];
      for (var b = 0; b < this.length; b++) {
        if(a.indexOf(this[b]) == -1) {
          a.push(this[b])
      return a
    
  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;
    function lowestUnique(str) {
      var arr = str.split(" ");
      var length = arr.length;
      var mapObj = {};
      var uniqueArr = arr.unique();
      for (var i = 0; i < length; i++) {
        mapObj[arr[i]] = 1 + (mapObj[arr[i]] || 0);
      var lowestNum = 100000000000;
      for (var n = 0; n < uniqueArr.length; n++) {
        if (mapObj[uniqueArr[n]] == 1) {
          if (lowestNum > uniqueArr[n]) {
            lowestNum = uniqueArr[n];
      if (lowestNum === 100000000000) {
        console.log(0);
      } else {
        arr.filter(function(e, ind, array) {
          if (e == lowestNum) {
            console.log(ind + 1);
        });
    
  5. unique()
    Array.prototype.unique = function() {
      var json = {};
      var res = [];
      for(var i = 0;i < this.length;i++) {
        if(!json[this[i]]) {
          res.push(this[i]);
          json[this[i]] = true;
      console.log(json);
      return res;
    var a = [1,2,3,3,2,1,4,4,0,7,7,7,7,7];
    console.log(a.unique());
    
  6. unique()
    Array.prototype.unique = function(){
        var temp = {};
        for (var i = 0; i < this.length; i++)
            temp[this[i]] = true;
        var r = [];
        for (var k in temp)
            r.push(k);
        return r;
    
  7. unique()
    Array.prototype.unique = function () {
      var tmp = {}, result = [];
      for (var i = 0; i < this.length; i++) {
        if (!tmp[this[i]]) {
          result.push(this[i]);
          tmp[this[i]] = true;
      return result;
    ...
    
  8. unique()
    var fs = require('fs');
    Array.prototype.unique = function() {
      var res = [],
        hash = {};
      for (var i = 0, elem;
        (elem = this[i]) != null; i++) {
        if (!hash[elem]) {
          res.push(elem);
          hash[elem] = true;
    ...
    
  9. unique()
    Array.prototype.unique = function() {
        var newArr = [],
            origLen = this.length,
            found, x, y;
        for (x = 0; x < origLen; x++) {
            found = undefined;
            for (y = 0; y < newArr.length; y++) {
                if (this[x] === newArr[y]) {
                    found = true;
    ...