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 493959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:27:12+00:00 2026-05-13T05:27:12+00:00

As everybody knows there’s no built-in function to remove the duplicates from an array

  • 0

As everybody knows there’s no built-in function to remove the duplicates from an array in javascript. I’ve noticed this is also lacking in jQuery (which has a unique function for DOM selections only), and the most common snippet I found checks the entire array and a subset of it for each element (not very efficient I think), like:

for (var i = 0; i < arr.length; i++)
    for (var j = i + 1; j < arr.length; j++)
        if (arr[i] === arr[j])
            //whatever

so I made my own:

function unique (arr) {
    var hash = {}, result = [];
    for (var i = 0; i < arr.length; i++)
        if (!(arr[i] in hash)) { //it works with objects! in FF, at least
            hash[arr[i]] = true;
            result.push(arr[i]);
        }
    return result;
}

I wonder if there’s any other algorithm accepted as the best for this case (or if you see any obvious flaw that could be fixed), or, what do you do when you need this in javascript (I’m aware that jQuery is not the only framework and some others may have this already covered).

  • 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-05-13T05:27:13+00:00Added an answer on May 13, 2026 at 5:27 am

    Using the object literal is exactly what I would do. A lot of people miss this technique a lot of the time, opting instead for typical array walks as the original code that you showed. The only optimization would be to avoid the arr.length lookup each time. Other than that, O(n) is about as good as you get for uniqueness and is much better than the original O(n^2) example.

    function unique(arr) {
        var hash = {}, result = [];
        for ( var i = 0, l = arr.length; i < l; ++i ) {
            if ( !hash.hasOwnProperty(arr[i]) ) { //it works with objects! in FF, at least
                hash[ arr[i] ] = true;
                result.push(arr[i]);
            }
        }
        return result;
    }
    
    // * Edited to use hasOwnProperty per comments
    

    Time complexities to summarize

      f()    | unsorted | sorted | objects | scalar | library
    ____________________________________________________________
    unique   |   O(n)   |  O(n)  |   no    |  yes   |    n/a
    original |  O(n^2)  | O(n^2) |   yes   |  yes   |    n/a
    uniq     |  O(n^2)  |  O(n)  |   yes   |  yes   | Prototype
    _.uniq   |  O(n^2)  |  O(n)  |   yes   |  yes   | Underscore
    

    As with most algorithms, there are trade offs. If you are only sorting scalar values, you’re modifications to the original algorithm give the most optimal solution. However, if you need to sort non-scalar values, then using or mimicking the uniq method of either of the libraries discussed would be your best choice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 537k
  • Answers 537k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Select count (VisitorID) from Company C where NOT EXISTS (select… May 17, 2026 at 1:39 am
  • Editorial Team
    Editorial Team added an answer I wouldn't say bad as it will depend on the… May 17, 2026 at 1:39 am
  • Editorial Team
    Editorial Team added an answer The real difference is like the difference between an ASP.NET… May 17, 2026 at 1:39 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Ok maybe everybody knows how to do this, but I've never try it beacause
As everybody working with Oracle knows, it an empty Varchar2 will result in a
Possible Duplicate: Null object in javascript Hi, I've read this thread about null in
Sorry, I'm not even sure how to ask this exactly... but I wrote a
Anyone knows how can i start to develop a multitenant site in MVC2, in
Hello everybody i hope everybody is doin well, Actually i have a table in
I'd like to mark a method as deprecated, so the people using it can
What are the most strange/sophisticated/surprising/deeply hidden software vulnerabilities or exploits you have ever seen?
I'm wondering about some ideas which can improve process of designing solutions using Access
Earlier versions of IDEA had their own skin and looked really nice. But fresh

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.