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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:50:35+00:00 2026-06-07T07:50:35+00:00

What is the optimal way to get unique strings in an array? There are

  • 0

What is the optimal way to get unique strings in an array? There are times when it makes sense to do it one of many ways, here are 3:

  1. creating an array and, for each new item you push, first check if _.indexOf(array, newItem) == -1
  2. creating a hash, with all values as true, such as {key1: true, key2: true}, then _.keys(hash)
  3. pushing all items in the array, then running keys = _.uniq(keys)

The above code is using underscore.js helpers.

Having knowledge of the internals of JavaScript constructs/vm’s, and some formal algorithms knowledge, would probably make this a no brainer but I’m not there yet. I’m sure it differs from browser to browser (and node), but maybe there is a preferred approach. Any ideas?

  • 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-07T07:50:37+00:00Added an answer on June 7, 2026 at 7:50 am

    The first solution has to loop through every element of the array, for each element of the array. That makes a complexity of O(n²).

    The second is probably the best, because it only loops through the array, then loops through the keys. That’s basically O(2n), which is just O(n).

    The third depends on how efficient uniq() is. For instance, it might well just be an implementation of method 2.

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

Sidebar

Related Questions

I am trying to find out if there is a more optimal way for
This method seems stupid and a bit heavy; is there a more optimal way
I was wondering if there is an easy way to get the current time
Is there a better way to use tr/// on each element in an array
Possible Duplicates: Generating Random Numbers in Objective-C What's the most optimal way to get
I am looking to see if there is a smart or optimal way to
There's a common way to store multiple values in one variable, by using a
What would be the optimal way to develop a basic graphical application for Windows
What would be the optimal way to match that all of a set of
I was told that the optimal way to program in C++ is to use

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.