【发布时间】:2017-05-09 14:25:15
【问题描述】:
我有这段代码可以在我的数组中查找重复出现的值。我有 81 个文本框形成一个网格,它们位于 9 行,每行 9 个框中。我早些时候在我的代码中将它们全部保存到一个包含 81 个元素的一维数组中。我在另一个问题上找到了其中的一些代码:Finding duplicate integers in an array and display how many times they occurred,它对我有用,但我找不到它实际上是哪个数组元素重复出现的。
int[] OrigValues = new int[];//Already defined earlier, and assigned.
for (int c = 1; c <= 9; c++) //in this case, I called my int c instead of the usual i
{
Console.WriteLine("Row {0}:", c);
var dict = new Dictionary<int, int>();
foreach (var value in OrigValues.SubArray(c * 9 -9, 9))
{
if (dict.ContainsKey(value))
dict[value]++;
else
dict[value] = 1;
}
foreach (var pair in dict)
{
Console.WriteLine("Value {0} occurred {1} times.", pair.Key, pair.Value);
if (pair.Value >= 2 && pair.Key != 0)
{
//I have no way of finding which 2 array slots were the ones that had the same value in each of these rows.
}
}
}
OrigValues.SubArray 是一种扩展方法,它的作用类似于子字符串,除了它用于数组,从索引开始获取数组元素,并获取一个长度(那里,c* 9 - 9 是我的索引,而 9 是我的长度)
【问题讨论】: