Reputation: 23
int[] a = new int[] { 1, 2, 3, 4 };
int[] b = new int[] { 5, 6, 1, 2, 7, 8 };
int Count = 0;
for (int i = 0; i < a.Length; i++)
{
for (int j = 0; j < b.Length; j++)
{
if (a[i] == b[j])
{
Count++;
break;
}
}
}
Console.WriteLine(Count);
Console.ReadLine();
Above is a simple program which can search through both arrays and find duplicates between them. However I am having difficulty when making the first array a 2D array as I get an error running the code below 'Index was outside the bounds of the array.' I really can't figure out what to do so I would be grateful for any help.
int[][] a = {new int[] { 1, 2, 3, 4 }, new int[] { 3, 9, 9 }};
int[] b = new int[] { 5, 6, 1, 2, 7, 8 };
int Count = 0;
for (int i = 0; i < a.Length; i++)
{
for (int j = 0; j < b.Length; j++)
{
if (b[j] == a[i][j])
{
Count++;
break;
}
}
}
Console.WriteLine(Count);
Console.ReadLine();
Upvotes: 2
Views: 3647
Reputation: 494
There's a much simpler way still. Convert the 2D (or more) array to 1D then use a regular SequenceEqual
to compare them.
static bool AreEqual<T>(T[,] array1, T[,] array2)
{
return array1.Cast<T>().SequenceEqual(array2.Cast<T>());
}
Upvotes: 0
Reputation: 9500
Your array a
is actually an array with 2 elements, one that is 4 elements long, and the other that is 3 elements long. Your inner loop is iterating over each of those arrays for every element of b
, so that when it hits the 5th element of b
, b[4]
compared against a[0][4]
, it's out of bounds, since the last element of a[0]
is a[0][3]
, but you are trying to compare to a[0][4]
. Also, do you really want to break
after you've found a match?
If you want to find all duplicates, the simplest bets are probably either @peyman, or you might use the idea of commenter @zerkms, Enumerable.Intersect<TSource>
, assuming flattening a into 1d first.
int[] flattened = a.SelectMany(item => item).ToArray();
Count = b.Intersect(flattened).Count();
which is the same as:
b.Intersect(a.SelectMany(item => item).ToArray()).Count();
Upvotes: 0
Reputation: 2392
I assume you want to find duplicates between the 2d array and a 1d array.
int[][] a = { new int[] { 1, 2, 3, 4 }, new int[] { 3, 9, 9 } };
int[] b = new int[] { 5, 6, 1, 2, 7, 8 };
int Count = 0;
for (int h = 0; h < a.Length; h++)
{
for (int i = 0; i < a[h].Length; i++)
{
for (int j = 0; j < b.Length; j++)
{
if (b[j] == a[h][i])
{
Count++;
break;
}
}
}
}
Or in Linq ;)
int Count = (from i in a from j in i from k in b where k == j select j).Count();
= Found 2 duplicates between a2 and b?
Edit for new spec
2nd edit for storing duplicates for each 2d array element.
var duplicates = new List<int>();
foreach (var i in a)
{
var duplicate = 0;
foreach (var j in i)
{
foreach (var k in b)
{
if (k == j)
{
duplicate++;
}
}
}
duplicates.Add(duplicate);
}
Or linq again ;)
var duplicates = a.Select(i => (from j in i from k in b where k == j select j).Count()).ToList();
Update 3: For your selected code format:
var duplicates = new List<int>();
for (int h = 0; h < a.Length; h++)
{
var duplicate = 0;
for (int i = 0; i < a[h].Length; i++)
{
for (int j = 0; j < b.Length; j++)
{
if (b[j] == a[h][i])
{
duplicate++;
break;
}
}
}
duplicates.Add(duplicate);
}
Added duplicate output:
for (int d = 0; d < duplicates.Count; d++)
{
Console.WriteLine(duplicates[d]);
}
Upvotes: 1
Reputation: 3136
You can use Linq expression as well, I guess it's much easier than loop
a.SelectMany(x => x).Count(x => b.Contains(x))
Upvotes: 1
Reputation: 115
You can try with a foreach
foreach (var val1 in a)
{
foreach (var val2 in val1)
{
foreach (var val3 in b)
{
if (val3 == val2)
{
Count++;
}
}
}
}
Or with a another for
for (int i = 0; i < a.Length; i++)
{
var innerArray = a[i];
for (int f = 0; f < innerArray.Length; f++)
{
for (int j = 0; j < b.Length; j++)
{
if (b[j] == innerArray[f])
{
Count++;
}
}
}
}
Upvotes: 1
Reputation: 1096
//initialize stuff here
for(int i = 0; i<a.length; ++i) {//iterate over the rows in a
for(int j = 0; j<a[i].length; ++j) {//iterate over columns in a
for(int k = 0; k < b.length; ++k) {//iterate over b
if(a[i][j] == b[k]) {
//increment stuff here
}//end if
}//end for k
}//end for j
//print stuff for each sub array here
}end for i
//print stuff here for all sub arrays
Upvotes: 1