我正在编写一段代码,用于从数组中打印出重复的整数及其出现次数。我不允许使用LINQ,只能使用简单的代码。我认为我已经接近成功了,但是对如何获得正确的输出感到困惑:
class Program
{
static void Main(string[] args)
{
int[] array = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12 };
int count = 1;
for (int i = 0; i < array.Length; i++)
{
for (int j = i; j < array.Length - 1 ; j++)
{
if(array[j] == array[j+1])
count = count + 1;
}
Console.WriteLine("\t\n " + array[i] + "occurse" + count);
Console.ReadKey();
}
}
}
TryGetValue()
模式更易读,而且更高效(对于读取,只需要查找一次存在的值而不是两次),所以我已经进行了编辑,而不仅仅是提出建议。如果你真的不喜欢,可以随时恢复原样。 - Peter Duniho