'How to find or count the duplicate in multidimensional array in c# [closed]
I am creating a 2d (3 * 3)
array in c# which has to count or find the duplicate value
int[,] arr = new int[3, 3] {
{1, 2, 6},
{4, 1, 5},
{6, 1, 8}
};
I expected the output has 1
and 6
are the duplicate values
For beginners only with for
loop
Solution 1:[1]
You can try HashSet<int>
, e.g.
int[,] arr = new int[3, 3] {
{1, 2, 6 },
{4, 1, 5 },
{6, 1, 8 }
};
HashSet<int> unique = new HashSet<int>();
foreach (var item in arr)
if (!unique.Add(item))
Console.WriteLine(item); // Not unique, print it out
Outcome:
1
6
1
If we want to print each duplicate once, we can add another HashSet<int>
:
HashSet<int> unique = new HashSet<int>();
HashSet<int> duplicates = new HashSet<int>();
foreach (var item in arr)
if (!unique.Add(item))
duplicates.Add(item);
foreach (var item in duplicates)
Console.WriteLine(item);
Finally, if you want to count duplicate occurences we can change HashSet<int> duplicates
into Dictionary<int, int> duplicates
:
HashSet<int> unique = new HashSet<int>();
Dictionary<int, int> duplicates = new Dictionary<int, int>();
foreach (var item in arr)
if (!unique.Add(item))
if (duplicates.TryGetValue(item, out int count))
duplicates[item] = count + 1;
else
duplicates[item] = 2;
foreach (var item in duplicates)
Console.WriteLine($"{item.Key} appears {item.Value} times");
Edit: you can change foreach
loop into nested for
ones, e.g:
All duplicates
for (int i = 0; i < arr.GetLength(0); ++i)
for (int j = 0; j < arr.GetLength(1); ++j)
if (!unique.Add(arr[i, j]))
Console.WriteLine(arr[i, j]);
Distinct duplicates
HashSet<int> duplicates = new HashSet<int>();
for (int i = 0; i < arr.GetLength(0); ++i)
for (int j = 0; j < arr.GetLength(1); ++j)
if (!unique.Add(arr[i, j]))
if (duplicates.Add(arr[i, j])) // print distinct duplicate only
Console.WriteLine(arr[i, j]);
Solution 2:[2]
Perhaps like this:
var arr = new int[3, 3]{{1,2,6}, {4,1,5}, {6,1,8}};
var duplicates = arr
.Cast<int>()
.GroupBy(n => n)
.Where(g => g.Count() > 1)
.Select(g => g.Key)
.ToArray();
.Cast<int>()
makes the array usable for LINQ, .GroupBy(n => n)
groups numbers by their value, .Where(g => g.Count() > 1)
counts the number of items in the group, .Select(g => g.Key)
returns only the group keys - the original values.
Do .Where(g => g.Count() > 1).Select(g => g.Count())
to return the counts of each, or just operate on the groups as needed.
Solution 3:[3]
Not very efficient, but you could also store your counts in a dictionary, then print the keys that have a count of more than 1:
var counts = new Dictionary<int, int>();
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
var number = arr[i, j];
if (!counts.ContainsKey(number))
{
counts[number] = 0;
}
counts[number] += 1;
}
}
foreach (var pair in counts)
{
if (pair.Value > 1)
{
Console.WriteLine(pair.Key);
}
}
// 1
// 6
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Patrick |
Solution 3 | RoadRunner |