Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking to see if last 2 numbers in array both exist in 2D array using loops

Tags:

arrays

c#

I have two arrays, one singular and the other 2 dimensional.

int[][] array1 = { 
    new int [] {1, 22, 3, 44, 5, 66},
    new int [] {11, 22, 33, 44, 55, 66},
    new int [] {1, 2, 3, 4, 5, 6},
};

int[] array2 = new int[] {1, 2, 3, 5, 66}

I need to create a loop which searches the array1 for both the 2nd last digits in array2, so it would return how many times an array with array1 contains both 5 and 66, which is 1, as the other two only contain 1 of each number.

I already managed to write a function which returns how many times array2 as a whole exists in array1, this new function is effectively a refinement of that.

for (int a = 0; a < array1[i].Length; a++) 
{                   
    for (int b = 0; b < array2.Length; b++)
    {   
        if (array2[c] == array1[a][b])                            
            count++;

        temp[b] = array1[a][b];
    }
}

I feel all would be needed to search for just the last two digits is a slight change to this function, I tried to add in another loop but that didn't work either. How would I go about doing this? I'm using loops and not Contains for a reason since i'm still learning the basics.

like image 940
Robert Paulson Avatar asked Jan 24 '26 00:01

Robert Paulson


2 Answers

The one thing is not clear in question that, does it matter which position the two digits occur in the 2D array? If ithis is not the case, then you can use Intersect() which produces the set intersection of two sequences by using the default equality comparer to compare values:

var result = array1.Count(x => x.Intersect(array2.Reverse().Take(2)).Count() == 2);

If you have paid an attention we have used this line, for getting last two elements of array1:

array1.Reverse().Take(2);

.NET Fiddle

Additional:

If you want to find if last two elements of arrays in 2D array is equal to last two elements of array1, then you can try LINQ solution:

var result = array1.Count(x=> x.Reverse().Take(2).SequenceEqual(array2.Reverse().Take(2)));

Explanation of used extension methods:
Reverse() inverts the order of the elements in a sequence.
Take() returns a specified number of contiguous elements from the start of a sequence.
SequenceEqual() determines whether two sequences are equal by comparing the elements by using the default equality comparer for their type.

After getting last two elements of both arrays, we will use SequenceEqual() to determine if both arrays are equal.

like image 168
Farhad Jabiyev Avatar answered Jan 26 '26 15:01

Farhad Jabiyev


var res = array1.Where((x) => (x.Contains(array2.Last()) && x.Contains(array2[array2.Length - 2]))).Count();

Explaination:

  • array1.Where takes every subarray of array1 and filters the ones that meet a certain condition.
  • The condition being every subarray of array1 contains the last && next-to-last element of array2.
  • The Count() methods returns the number of subarrays that meet the conditions
like image 22
Lyubomir Papazov Avatar answered Jan 26 '26 15:01

Lyubomir Papazov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!