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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:15:56+00:00 2026-06-05T00:15:56+00:00

I need to find the intersection of two sorted integer arrays and do it

  • 0

I need to find the intersection of two sorted integer arrays and do it very fast.

Right now, I am using the following code:

int i = 0, j = 0;

while (i < arr1.Count && j < arr2.Count)
{
    if (arr1[i] < arr2[j])
    {
        i++;
    }
    else
    {
        if (arr2[j] < arr1[i])
        {
            j++;
        }
        else 
        {
            intersect.Add(arr2[j]);
            j++;
            i++;
        }
    }
}

Unfortunately it might to take hours to do all work.

How to do it faster? I found this article where SIMD instructions are used. Is it possible to use SIMD in .NET?

What do you think about:

http://docs.go-mono.com/index.aspx?link=N:Mono.Simd Mono.SIMD

http://netasm.codeplex.com/ NetASM(inject asm code to managed)

and something like http://www.atrevido.net/blog/PermaLink.aspx?guid=ac03f447-d487-45a6-8119-dc4fa1e932e1

 

EDIT:

When i say thousands i mean following (in code)

for(var i=0;i<arrCollection1.Count-1;i++)
{
    for(var j=i+1;j<arrCollection2.Count;j++)
    {
        Intersect(arrCollection1[i],arrCollection2[j])  
    }
}
  • 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-05T00:15:58+00:00Added an answer on June 5, 2026 at 12:15 am

    UPDATE

    The fastest I got was 200ms with arrays size 10mil, with the unsafe version (Last piece of code).

    The test I’ve did:

    var arr1 = new int[10000000];
    var arr2 = new int[10000000];
    
    for (var i = 0; i < 10000000; i++)
    {
        arr1[i] = i;
        arr2[i] = i * 2;
    }
    
    var sw = Stopwatch.StartNew();
    
    var result = arr1.IntersectSorted(arr2);
    
    sw.Stop();
    
    Console.WriteLine(sw.Elapsed); // 00:00:00.1926156
    

    Full Post:

    I’ve tested various ways to do it and found this to be very good:

    public static List<int> IntersectSorted(this int[] source, int[] target)
    {
        // Set initial capacity to a "full-intersection" size
        // This prevents multiple re-allocations
        var ints = new List<int>(Math.Min(source.Length, target.Length));
    
        var i = 0;
        var j = 0;
    
        while (i < source.Length && j < target.Length)
        {
            // Compare only once and let compiler optimize the switch-case
            switch (source[i].CompareTo(target[j]))
            {
                case -1:
                    i++;
    
                    // Saves us a JMP instruction
                    continue;
                case 1:
                    j++;
    
                    // Saves us a JMP instruction
                    continue;
                default:
                    ints.Add(source[i++]);
                    j++;
    
                    // Saves us a JMP instruction
                    continue;
            }
        }
    
        // Free unused memory (sets capacity to actual count)
        ints.TrimExcess();
    
        return ints;
    }
    

    For further improvement you can remove the ints.TrimExcess();, which will also make a nice difference, but you should think if you’re going to need that memory.

    Also, if you know that you might break loops that use the intersections, and you don’t have to have the results as an array/list, you should change the implementation to an iterator:

    public static IEnumerable<int> IntersectSorted(this int[] source, int[] target)
    {
        var i = 0;
        var j = 0;
    
        while (i < source.Length && j < target.Length)
        {
            // Compare only once and let compiler optimize the switch-case
            switch (source[i].CompareTo(target[j]))
            {
                case -1:
                    i++;
    
                    // Saves us a JMP instruction
                    continue;
                case 1:
                    j++;
    
                    // Saves us a JMP instruction
                    continue;
                default:
                    yield return source[i++];
                    j++;
    
                    // Saves us a JMP instruction
                    continue;
            }
        }
    }
    

    Another improvement is to use unsafe code:

    public static unsafe List<int> IntersectSorted(this int[] source, int[] target)
    {
        var ints = new List<int>(Math.Min(source.Length, target.Length));
    
        fixed (int* ptSrc = source)
        {
            var maxSrcAdr = ptSrc + source.Length;
    
            fixed (int* ptTar = target)
            {
                var maxTarAdr = ptTar + target.Length;
    
                var currSrc = ptSrc;
                var currTar = ptTar;
    
                while (currSrc < maxSrcAdr && currTar < maxTarAdr)
                {
                    switch ((*currSrc).CompareTo(*currTar))
                    {
                        case -1:
                            currSrc++;
                            continue;
                        case 1:
                            currTar++;
                            continue;
                        default:
                            ints.Add(*currSrc);
                            currSrc++;
                            currTar++;
                            continue;
                    }
                }
            }
        }
    
        ints.TrimExcess();
        return ints;
    }
    

    In summary, the most major performance hit was in the if-else’s.
    Turning it into a switch-case made a huge difference (about 2 times faster).

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

Sidebar

Related Questions

using GLScene in delphi I need to find the intersection between an object (a
Need a Java function to find intersection of two strings. i.e. characters common to
which is the fastest way to find the union and intersection between two lists?
I need to find out the intersecting points of two circles. I have the
i need to find few words or matching pattern using a Javascript. this is
in order to find an intersection of multiple users' settings (boolean), I need to
Need to find the timestamp for the first minute of the first day of
I need to find a way to spin off a thread from a static
I need to find a way to call a vb.net function in my aspx
I need to find/create a library that can load hdr images in many formats

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.