MANnDAaR
MANnDAaR

Reputation: 2590

Sort array in ascending order while eleminating repeated values

I'm working on project where I have array simmilar to this

var sortBp = [ 'height58em', 'width480px', 'width768px', 'width959px', 'width767px', 'width767px' ];

I want to sort this array in ascending order while eleminating repeated values so that result should be

var sortBp = [ 'height58em', 'width480px', 'width767px', 'width768px', 'width959px' ];

I'm using following function to sort array in ascending array but how do I eliminate immediate values ?? (In above case 'width767px')

var sortBp = bpArrays.sort(function(a, b) {
                    a = a.replace(/[a-z]/g, '');
                    b = b.replace(/[a-z]/g, '');
                    return a - b;
}); 

Upvotes: 1

Views: 513

Answers (4)

MANnDAaR
MANnDAaR

Reputation: 2590

Found It !!

var sortBp = [ 'height58em', 'width480px', 'width768px', 'width959px', 'width767px', 'width767px' ];

uniqueArray = sortBp.filter(function(elem, pos) {
        return sortBp.indexOf(elem) == pos;
});

EDIT :

I have found another solution here : http://dreaminginjavascript.wordpress.com/2008/08/22/eliminating-duplicates/

function eliminateDuplicates(arr) {
      var i, len=arr.length, out=[], obj={};
      for (i=0;i<len;i++) {
        obj[arr[i]]=0;
      }
      for (i in obj) {
        out.push(i);
      }
      return out;
}

Upvotes: 0

bguiz
bguiz

Reputation: 28587

If this is not homework, i.e. you aren't required to implement the algorithm yourself, you could use a combination of sortBy and uniq functions.

sortBp = [ 'height58em', 'width480px', 'width768px', 'width959px', 'width767px', 'width767px' ];
_.uniq(_.sortBy(sortBp, function(x) {return x;}), true);

This returns:

["height58em", "width480px", "width767px", "width768px", "width959px"]

Note that this will be sort by lexical order, and if you do not wish this to happen, simply substitute the identity function function(x) {return x;} with your own logic.

Upvotes: 1

Lonely
Lonely

Reputation: 613

Array.prototype.contains = function(k) {
for ( var p in this)
if (this[p] === k)
  return true;
return false;
};




inputArray.sort();

function uniqueArray(inputArray){
result=[];
for(var i in inputArray){
if(!result.contains(inputArray[i])){
result.push(inputArray[i]);
}
}
return result;
}

Upvotes: 0

Mics
Mics

Reputation: 1430

Firstly, you can't eliminate elements while sorting. You have to sort the array first, then remove duplicates. Your solution using Array.prototype.filter and Array.prototype.indexOf might be good for unsorted array, but since your array is sorted, it's overhead here(takes O(n) for each). Instead you can just loop through the array and compare element with previous one.

function uniq(array) {
  var i, l = array.length, result = [];
  for (i = 0; i < l; i++) {
    if (result[result.length - 1] != array[i]) {
      result.push(array[i]);
    }
  }
  return result;
}

This is same with Underscore and Prototype's uniq() implemention.

Last note: remember that this will work fine with sorted array only.

Upvotes: 1

Related Questions