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

  • Home
  • SEARCH
  • 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 6768397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:04:13+00:00 2026-05-26T15:04:13+00:00

I have a double array x and a double array y. Both can have

  • 0

I have a double array x and a double array y. Both can have duplicates elements.

const double MAX = 10000.0;
const int X_LENGTH = 10000;
const int Y_LENGTH = 10000;
const double TOLERANCE = 0.01;

Random random = new Random();

double[] x = new double[X_LENGTH];
for(int i = 0; i < X_LENGTH; i++)
{
        x[i] = MAX * random.NextDouble();
}

double[] y = new double[Y_LENGTH];
for(int j = 0; j < Y_LENGTH; j++)
{
        y[j] = MAX * random.NextDouble();
}

I am trying to count how many elements in array x are found in array y within a tolerance, and how many elements in array y are found in array x within the same tolerance. Note that these numbers can be different. The simplest way to do this is with two sets of two embedded loops:

int x_matches = 0;
for(int i = 0; i < X_LENGTH; i++)
{
        for(int j = 0; j < Y_LENGTH; j++)
        {
                if(Math.Abs(x[i] - y[j]) <= TOLERANCE)
                {
                        x_matches++;
                        break;
                }
        }
}

int y_matches = 0;
for(int j = 0; j < Y_LENGTH; j++)
{
        for(int i = 0; i < X_LENGTH; i++)
        {
                if(Math.Abs(x[i] - y[j]) <= TOLERANCE)
                {
                        y_matches++;
                        break;
                }
        }
}

However, this code is run thousands of times and is the main bottleneck in the software. I am trying to speed it up. I have already optimized by sorting both arrays first and then asynchronously iterating through them.

Array.Sort(x);
Array.Sort(y);

int x_matches_2 = 0;
int i2 = 0;
int j2 = 0;
while(i2 < X_LENGTH && j2 < Y_LENGTH)
{
        if(Math.Abs(x[i2] - y[j2]) <= TOLERANCE)
        {
                x_matches_2++;
                i2++;
        }
        else if(x[i2] < y[j2])
        {
                i2++;
        }
        else if(x[i2] > y[j2])
        {
                j2++;
        }
}

int y_matches_2 = 0;
int i3 = 0;
int j3 = 0;
while(i3 < X_LENGTH && j3 < Y_LENGTH)
{
        if(Math.Abs(x[i3] - y[j3]) <= TOLERANCE)
        {
                y_matches_2++;
                j3++;
        }
        else if(x[i3] < y[j3])
        {
                i3++;
        }
        else if(x[i3] > y[j3])
        {
                j3++;
        }
}

I am wondering if anybody knows of a way to merge these two loops into one and still obtain the same answer. I can only come up with this:

int x_matches_2 = 0;
int y_matches_2 = 0;
bool[] y_matched = new bool[Y_LENGTH];
for(int i = 0; i < X_LENGTH; i++)
{
        bool x_matched = false;

        for(int j = 0; j < Y_LENGTH; j++)
        {
                if(Math.Abs(x[i] - y[j]) <= TOLERANCE)
                {
                        if(!x_matched)
                        {
                                x_matches_2++;
                                x_matched = true;
                        }
                        if(!y_matched[j])
                        {
                                y_matches_2++;
                                y_matched[j] = true;
                        }
                }
        }
}

It doesn’t require sorting; however, it ends up being slower because more comparisons must be done.

P.S. This is an oversimplification of my actual problem, but I think the solution to this will apply to both.

  • 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-26T15:04:14+00:00Added an answer on May 26, 2026 at 3:04 pm

    It is possible to have a single loop, but you will process some part of each array more than once.

    public static void JustDoIt(double[] x, double[] y)
    {
        Array.Sort(x);
        Array.Sort(y);
    
        bool mustContinue = true;
        bool isXTurns;
        bool withinTolerance;
    
        int lastBase_x = 0;
        int lastBase_y = 0;
    
        int lastMatch_x = 0;
        int lastMatch_y = 0;
    
        int current_x = 0;
        int current_y = 0;
    
        int matchedCount_x = 0;
        int matchedCount_y = 0;
    
        double yourTolerance = 0.001;
    
        while (mustContinue)
        {
            isXTurns = x[current_x] <= y[current_y];
    
            if (isXTurns)
            {
                withinTolerance = (y[current_y] - x[current_x] <= yourTolerance);
            }
            else
            {
                withinTolerance = (x[current_x] - y[current_y] <= yourTolerance);
            }
    
            if (withinTolerance)
            {
                if (isXTurns)
                {
                    if (current_x > lastMatch_x)
                    {
                        matchedCount_x++;
                        lastMatch_x = current_x;
                    }
    
                    if (current_y > lastMatch_y)
                    {
                        matchedCount_y++;
                        lastMatch_y = current_y;
                    }
    
                    if (current_y + 1 < y.Length)
                    {
                        current_y++;
                    }
                    else if (current_x + 1 < x.Length)
                    {
                        current_x++;
                    }
                    else
                    {
                        mustContinue = false;
                    }
    
                }
                else
                {
                    if (current_y > lastMatch_y)
                    {
                        matchedCount_y++;
                        lastMatch_y = current_y;
                    } 
    
                    if (current_x > lastMatch_x)
                    {
                        matchedCount_x++;
                        lastMatch_x = current_x;
                    }
    
                    if (current_x + 1 < x.Length)
                    {
                        current_x++;
                    }
                    else if (current_y + 1 < y.Length)
                    {
                        current_y++;
                    }
                    else
                    {
                        mustContinue = false;
                    }
                }
    
            }
            else
            {
                if (isXTurns)
                {
                    lastBase_x++;
                    mustContinue = lastBase_x < x.Length;
                }
                else
                {
                    lastBase_y++;
                    mustContinue = lastBase_y < y.Length;
                }
    
                current_x = lastBase_x;
                current_y = lastBase_y;
            }
        }
    }
    

    Some odd results you’ll get : if you have 2 arrays of 2 elements each, it’s possible that you have more than 2 match from x to y or y to x. It happens cause x[0] can match with y[0] ans y[1], so can x[1]. This way, you’d end up with 4 match in both “direction”. For example, when I ran this code with 2 arrays of 1000 items each, I had 1048 matches in one, and 978 in the other. I hope it helps.

    Edit: Here is a generic version :

    public static void JustDoIt<T>(IEnumerable<T> items_x, IEnumerable<T> items_y, out int matchedCount_x, out int matchedCount_y, IComparer<T> comparer, Func<T, T, bool> toleranceReferee)
    {
    
        T[] x = items_x.OrderBy(item => item, comparer).ToArray();
        T[] y = items_y.OrderBy(item => item, comparer).ToArray();
    
        bool mustContinue = true;
        bool isXTurns;
        bool withinTolerance;
    
        int lastBase_x = 0;
        int lastBase_y = 0;
    
        int lastMatch_x = 0;
        int lastMatch_y = 0;
    
        int current_x = 0;
        int current_y = 0;
    
        matchedCount_x = 0;
        matchedCount_y = 0;
    
        while (mustContinue)
        {
            isXTurns = comparer.Compare(x[current_x], y[current_y]) <= 0;
    
            withinTolerance = toleranceReferee(x[current_x], y[current_y]);
    
            if (withinTolerance)
            {
                if (isXTurns)
                {
                    if (current_x > lastMatch_x)
                    {
                        matchedCount_x++;
                        lastMatch_x = current_x;
                    }
    
                    if (current_y > lastMatch_y)
                    {
                        matchedCount_y++;
                        lastMatch_y = current_y;
                    }
    
                    if (current_y + 1 < y.Length)
                    {
                        current_y++;
                    }
                    else if (current_x + 1 < x.Length)
                    {
                        current_x++;
                    }
                    else
                    {
                        mustContinue = false;
                    }
    
                }
                else
                {
                    if (current_y > lastMatch_y)
                    {
                        matchedCount_y++;
                        lastMatch_y = current_y;
                    }
    
                    if (current_x > lastMatch_x)
                    {
                        matchedCount_x++;
                        lastMatch_x = current_x;
                    }
    
                    if (current_x + 1 < x.Length)
                    {
                        current_x++;
                    }
                    else if (current_y + 1 < y.Length)
                    {
                        current_y++;
                    }
                    else
                    {
                        mustContinue = false;
                    }
                }
    
            }
            else
            {
                if (isXTurns)
                {
                    lastBase_x++;
                    mustContinue = lastBase_x < x.Length;
                }
                else
                {
                    lastBase_y++;
                    mustContinue = lastBase_y < y.Length;
                }
    
                current_x = lastBase_x;
                current_y = lastBase_y;
            }
        }
    }
    

    With an example of how you’d call it for int :

    List<int> x2 = new List<int>() { 2, 4, 4, 6, 9, 9 };    // To test an IEnumerable
    IEnumerable<int> y2 = new int[] { 1, 3, 3, 4, 6, 9 };   // To test another
    
    int xcount;
    int ycount;
    
    SingleLoop.JustDoIt(
        x2,
        y2,
        out xcount,
        out ycount,
        Comparer<int>.Default,
       (currentX, currentY) => { return currentX == currentY; });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of a few million numbers. double* const data = new
I have an double array alist[1][1]=-1 alist2=[] for x in xrange(10): alist2.append(alist[x]) alist2[1][1]=15 print
I'm looking to slice a two dimensional array in C#. I have double[2,2] prices
I have an array of double pointers, but every time I try do print
In my program I have one array with 25 double values 0.04 When I
Let's say I have an array of lots of values (C++ syntax, sorry): vector<double>
Here is my code double hour_payload_add(int entries , double array[]) { int index=0 ,k=0;
I have a 2D array called results. Each row array in results contains both
Example: I have a function that works with vectors: double interpolate2d(const vector<double> & xvals,
I have a double[] array holding many numbers. I have an algorithm that selects

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.