Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This function sorts an array and returns the unique items. I'm trying to get it down to a readable 5 lines of code. So far I've renamed the variable cursor variable and inlined the assignments. I'll appreciate fresh eyes.

Before

function uniques(array) {
    var result = [], val, ridx;
    outer:
    for (var i = 0, length = array.length; i < length; i++) {
        val = array[i];
        ridx = result.length;
        while (ridx--) {
          if (val === result[ridx]) continue outer;
        }
        result.push(val);
    }
    return result;
}

After

function uniques(array) {
    var result = [], val, cursor;
    outer:
        for (var i = 0, length = array.length; i < length; i++) {
        val = array[i], cursor = result.length;
        while (cursor--) {
          if (val === result[cursor]) continue outer;
        }
        result.push(val);
    }
    return result;
}
share|improve this question
    
please change the title to what the code is doing. – ratchet freak Nov 20 '15 at 9:49

This function sorts an array and returns the unique items

  1. Remove duplicates from the array using Array#filter
  2. Sort the array using Array#sort

Code:

function uniques(arr) {
    // Remove the duplicate elements from the array
    return arr.filter(function (element, index, arr) {
        return arr.indexOf(element) === index;
    }).sort(function (a, b) {
        return a - b;
    });
}
share|improve this answer

This can be done as a 1 liner:

function uniques(arr) {
  return arr.sort().filter(function(x,i) { return x !== arr[i+1] });
}

All we're doing is sorting, then using filter to remove any element that is equal to its next adjacent element. Because the array is already sorted, it's guaranteed that duplicates will be next to one another.

Also note that this will run in O(n log n) time, and so will be faster than Tushar's answer, which filters first and then sorts -- the filtering step there will be O(n^2) because of the call to indexOf.

Finally, the above solution mutates the orginal array. If you'd like avoid that, simply add in a slice:

function uniques(arr) {
  return arr.slice(0).sort().filter(function(x,i) { return x !== arr[i+1] });
}
share|improve this answer
    
Filtering first will lower the number of operations to sort the array, thus improving the performance a bit – Tushar Nov 22 '15 at 5:58
    
That doesn't matter, because sorting is O(nlogn) and filtering the way you are doing it is O(n^2). The largest term will dominate and your method will be much slower for large n. – Jonah Nov 22 '15 at 6:15
    
you just blew my mind! – Rob Nov 22 '15 at 18:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.