Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9131221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:06:30+00:00 2026-06-17T08:06:30+00:00

Possible Duplicate: Javascript array sort and unique I was surprised to see that there

  • 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'];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:06:30+00:00Added an answer on June 17, 2026 at 8:06 am

    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' ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to sort an array of javascript objects? I have output that
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} AFAIK, there
Possible Duplicate: Length of Javascript Associative Array I have a JSON that looks like
Possible Duplicate: How to sort an array of javascript objects? Every time I mess
Possible Duplicate: Array-like object in javascript Javascript has native objects that are array-like objects
Possible Duplicate: Javascript Array.sort implementation? I want to know how .sort() works in JavaScript.
Possible Duplicate: How to sort an array of javascript objects? I have a array
Possible Duplicate: How to sort an array of javascript objects? i have a array
Possible Duplicate: javascript multidimensional array? Hi guys I need to create an array that
Possible Duplicate: Javascript Array.forEach HowTo break? Given that I have a forEach loop, how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.