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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:31:17+00:00 2026-05-18T04:31:17+00:00

I have two integer arrays which contain numeric values. I want to look through

  • 0

I have two integer arrays which contain numeric values. I want to look through both lists and check for commonality (or lack of) between the lists. I.e. I want to iterate through the array(s) and find those items which appear in both lists, while in a separate function I want to go through the arrays and find items which are in the first and not in the second.

The obvious way of doing this is nested for loops:

var containedInFirst = false;
for (var primaryID = 0; primaryID < PrimaryArray.length; primaryID++) {
        containedInFirst = false;
        for (var secondaryID = 0; secondaryID < SecondaryArray.length; secondaryID++) {
            if (PrimaryArray [primaryID] === SecondaryArray[secondaryID]) {
                containedInFirst = true;
                break;
            }
        }

//Do some more stuff based on the value of containedInFirst here
}

But given the lists could contain hundreds or thousands of records this is quite a bit of itteration and processor intensive.
I was therefore wondering if there is a more efficient way of executing the above code? Not just the actual searching, but something more efficient than an Integer array as the container for the values, or just not using nested for loops to traverse and compare the content.

Any thoughts on more efficient or elegant solutions?

  • 1 1 Answer
  • 1 View
  • 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-18T04:31:18+00:00Added an answer on May 18, 2026 at 4:31 am

    Sort them first and duck-waddle through them in parallel.

    a.sort();
    b.sort();
    
    left = []; both = []; right = []; 
    i = 0; j = 0;
    while (i < a.length && j < b.length) {
        if (a[i] < b[j]) {
            left.push(a[i]);
            ++i;
        } else if (b[j] < a[i]) {
            right.push(b[j]);
            ++j;
        } else {
            both.push(a[i]);
            ++i; ++j;
        }
    }
    while (i < a.length) {
        left.push(a[i]);
        ++i;
    }
    while (j < b.length) {
        right.push(b[j]);
        ++j;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You are given two sorted integer arrays, which have some common integers. Any common
I have two cell arrays, which are mixed string/integer type, e.g.: a={'a';1;'b'} b={'b';1} I
I have two arrays (a and b) with n integer elements in the range
I have two integer values, x and total. I am trying to find the
I have two columns in a table that are populated with integer values. For
Hey Guys I have four integer array lists which were created after parsing a
I have two arrays containing different keys and values. However, some keys store strings
i have two mutable arrays that i am looping through when the objects are
I have two arrays: char line[128] which is populated using: fgets(line,sizeof line, file) and
I have a site that dynamically creates two arrays of Lat/Long values based on

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.