Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I found a lot of answers online on how to check if two arrays are disjoint that involved the use of Enumerable.Intersect and Enumerable.Distinct calls, however I was wondering if there was a faster way to do what I need. I merely need to check if two arrays do not contain any common elements. I have three million arrays that I need to check with each other so I need to make a lot of calls and was wondering if there was any faster way to perform the following function:

C#
private static bool checkIfDisjoint(int[] a, int[] b)
{
      for(int i = 0; i < a.Length; i++)
      {
            for (int j = 0; j < b.Length; j++)
            {
                   if (a[i] == b[j])
                         return false;
            }
      }
      return true;
}


I need to make a lot of calls and each of my arrays are less than length 4. So I was wondering if there was a faster way to achieve this.

What I have tried:

I've tried LINQ and the code block above, and was wondering if given the fact that the length of my arrays are never more than 4, and I have only 180 different characters which the arrays can contain, there was something I could do to make this operation faster.
Posted
Updated 5-Feb-17 10:09am
v2
Comments
Peter_in_2780 4-Feb-17 16:41pm    
So an array could be re-expressed as a 180-bit bitmap with at most 4 ones. If you do that conversion once for each of your 3 million arrays, then the disjoint test is a simple AND operation. I suggest 3 long ints would be a suitable structure to hold the bitmaps.
Peter_in_2780 4-Feb-17 17:30pm    
Further to my previous comment, you will want to do everything you can to optimise the test. To check 3 x 10^6 items pairwise takes 4.5 x 10^12 operations, so each NANOsecond you can shave off the test loop will save you an hour and a quarter run time.
PIEBALDconsult 4-Feb-17 19:05pm    
How about building a Dictionary<int,HashSet<int[]>> ?
Then enumerate each array, putting elements in the Dictionary.
Then, given an element from array X, you can look whether or not the set of arrays that contain that element contains array Y.
Maciej Los 5-Feb-17 14:58pm    
My virtual 5!
Patrice T 5-Feb-17 16:54pm    
For best solution, we need to know detail about what can be each list, their contains.

1 solution

Hello,

As mentioned in the comment above by PIEBALDconsult you can use a HashSet.
What I would suggest is using the Overlaps() method that will return a boolean indicating the presence of identical elements.

HashSet<int> set = new HashSet<int>(array1);
bool a = set.Overlaps(array2);


Thanks.
 
Share this answer
 
Comments
Realhermit123 27-Mar-17 19:11pm    
But doesn't this only tell me if there is an overlap with any element in the set?
I need to specifically check if there is an overlap between two specific elements in the set.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900