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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:01:38+00:00 2026-05-13T22:01:38+00:00

TL DR; What is the most efficient way to sort and compare values in

  • 0

TL DR; What is the most efficient way to sort and compare values in a multiple arrays?

Okay, so we’ll assume a few constants to make this whole thing simple
var a = [1, 2, 3, 4, 5, 6], b = [0, 9, 8, 7, 6, 88, 99, 77], i, j;

Now if I wanted to see if any value in a is equal to any other value in b I’d have to sort through one of these arrays 6 times. That’s a lot of work, and it would seem there should be a more efficient way to do this. For those needing a visual aide here you are ( and yes I know about — and ++ I just don’t like to use ’em ):

for (i = a.length - 1; i > -1; i -= 1) {
    for (j = b.length - 1; j > -1; j -= 1) {
        if (a[i] === b[j]) {
            return b[j];
        }
    }
}

The B array gets ran through once for EACH element in A. Again, certainly there is a more efficient way to complete this task?

-Akidi

  • 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-13T22:01:39+00:00Added an answer on May 13, 2026 at 10:01 pm

    Depends on the size of your input arrays (several tradeoffs there)– your nested loops are simplest for small inputs like your examples.

    If you have huge arrays, and have control over their construction, consider keeping a map (Object in JS) around acting as a lookup set (if you’re creating a in a loop anyways, you can build the set in parallel.)

    var setA = {};
    for (int i = 0; i < a.length; i++) {
        setA[a[i]] = true;
    }
    

    Then you can see whether something exists in the set by just checking setA[?].

    Likewise with B, or with both together, etc, depending on your needs.

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

Sidebar

Related Questions

What is the most efficient way to sort a list, [0,0,1,0,1,1,0] whose elements are
What's the most efficient way to sort objects in an NSSet / NSMutableSet based
What would be the best and most efficient way to sort a Java Vector
What's the most efficient way to sort two vectors in lockstep in R? The
Possible Duplicate: What is the most efficient way to sort an NSSet? I currently
What is the most efficient way to get Elastic Map Reduce output into SimpleDB?
What is the most efficient way to drop a table in SAS? I have
What is the most efficient way for checking for equality of two m *
What is the most efficient way to set/re-order the TabIndex properties of many form
What is the fastest / most efficient way of getting all the distinct items

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.