0

Possible Duplicate:
Javascript array sort and unique

I was surprised to see that there is no built in jQuery function for that.

I've seen many solutions on stackoverflow, but the questions were polluted with not-working answers (to find a working one I had to test them all).

So, for future reference and to spare other users the trouble I decided to post this Q&A style.

How to return unique and sorted array values useing jQuery?

Numbers:

// input array
var inputArray = [10, 5, 15, 10, 5, 15];

// expected result array
var resultArray = [5, 10, 15];

Strings:

// input array
var inputArray = ['b', 'a', 'c', 'b', 'a', 'c'];

// expected result array
var resultArray = ['a', 'b', 'c'];
4
  • 1
    wooh you answered your own question at the same time ? Commented Jan 15, 2013 at 11:37
  • @Sibu That's totally legit on SO : if you solve a problem and have a solution that may help others, you can do what OP did. But in this specific case I don't think it's interesting enough as it's yet frequently answered. Commented Jan 15, 2013 at 11:39
  • @dystroy agreed but why ask an question when you know the answer, if you see OP question time and answer time, both are nearly same..amusing isn't it ? Commented Jan 15, 2013 at 11:43
  • The reason is to help other people : if the question is new and interesting and you think you have special knowledge, then you may bring it to the masses. See this. Commented Jan 15, 2013 at 11:50

3 Answers 3

1

I will take upon myself to add my answer here, instead of the previously asked question simply because the answer in the previously asked question is bad, but I don't hope for the author to alter the decision.

Now, to give you some more intuition into the solution that I propose: Sorting is O(n log n), removing duplicates is O(n), so we conclude that the entire operation should be no more complex then n log n. However, if you think of it, n will never increase, but will most likely decrease if you first remove duplicates, and then sort. So, while on the surface of it, it is still O(n log n), it will be generally faster. You could probably improve it (in other languages) by collecting values into a tree instead of a hash-table, but given the tremendous difference in performance between the "native" data structures and custom ones in JavaScript - the solution below should be optimal:

function sortUnique(array) {
    "use strict";
    var table = {}, key, i;
    for (i = 0; i < array.length; i++) {
        table[[array[i]]] = '';
    }
    i = 0;
    for (key in table) {
        array[i++] = key;
    }
    array.length = i;
    return array.sort();
}
sortUnique(['b', 'a', 'c', 'b', 'a', 'c']);
// [ 'a', 'b', 'c' ]
Sign up to request clarification or add additional context in comments.

4 Comments

Are you sure it's faster ? Especially for a not gigantic array ?
I will test your solution, if it works and is faster I will obviously mark it as correct, no need for comments like "but I don't hope for the author to alter the decision".
I'm marking this as accepted becouse it works and also i've learned a lesson about performance. tyvm wvxvw
If the array contains numbers or Dates, this alg converts them to strings in IE or node.js. The reason is (if I understand well) that table[[...]] is a nonstandard feature, present in ECMA6 draft only, not implemented in IE "yet" (2 years later than the above answer!). In ECMA5, object keys must always be converted to strings.
0

As a function:

function sort_unique(arr) {
    return arr.sort(function(a,b){
        return (a > b) ? 1 : -1;
    }).filter(function(el,i,a) {
        return (i==a.indexOf(el));
    });
}

1 Comment

I never said my solution was the fastest one. I posted a simple and working solution. The question was not "what is the fastest way to..."?
0

hope this will work for you

var dummy = [10, 5, 15, 10, 5, 15];
var arr = [];
$.map(arr, function(n, i){
  if($.inArray(n, arr) == -1)
      arr.push(n);
});

now in arr values are unique. now you can apply any sorting algo on this.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.