在数组中查找重复的值并输出它们

时间:2022-04-19 13:00:19

So I have some working code for outputting duplicate values in an array, however it is always one short when it outputs them on the screen and I know it has to do with the following code, but I just can't put my finger on it. Please note I can't use any System.Array.

我有一些用于输出数组中重复值的代码,但是当它在屏幕上输出值时总是短1,我知道这和下面的代码有关,但我不能把手指放在上面。请注意我不能使用任何System.Array。

for (column = 0; column < WinningScores.Length -1 ; column++) 
{
    if (WinningScores[column] == WinningScores[column + 1]) 
    {
        duplicateScore = WinningScores[column];
        duplicateIndex = column;
        Console.Write("\n Competitor {0} is the Winner with a total of: {1}", 
                      duplicateIndex + 1, 
                      duplicateScore - totalSum);
    }
}

3 个解决方案

#1


3  

You could try using LINQ for this:

你可以试试LINQ:

double[] WinningScores = new double[] { 4, 5, 3, 5 };

var duplicates =
    WinningScores
        .Select((score, index) => new { score, player = index + 1})
        .GroupBy(x => x.score, x => x.player)
        .Where(gxs => gxs.Count() > 1);

That gives me this result:

结果是:

在数组中查找重复的值并输出它们

You can see that it picked up the duplicate score of 5 with players 2 & 4.

你可以看到,玩家2和4的重复分数是5。

#2


1  

Your code looks for duplication in consécutive values. Try this code to output duplicate values in an array.

您的代码在连续的值中查找重复。尝试此代码在数组中输出重复的值。

  for (column = 0; column < WinningScores.Length -1 ; column++) 
     {

        for (int cl= column + 1 ; cl < WinningScores.Length - 1 ; cl++)
            {
               if (WinningScores[column] == WinningScores[cl]) {


                        duplicateScore = WinningScores[column];
                        duplicateIndex = column;
                        Console.Write("\n Competitor {0} is the Winner with a total  of: {1}", duplicateIndex + 1, duplicateScore - totalSum);
              }
             }
         }

#3


0  

//Starts loop through first element
for(int i = 0; i < arr.length, i++)

//starts at second element in array
for(int j = i + 1; k < arr.length, j++)

//double check my logic though, in a hurry at work so had to post this in a rush.

#1


3  

You could try using LINQ for this:

你可以试试LINQ:

double[] WinningScores = new double[] { 4, 5, 3, 5 };

var duplicates =
    WinningScores
        .Select((score, index) => new { score, player = index + 1})
        .GroupBy(x => x.score, x => x.player)
        .Where(gxs => gxs.Count() > 1);

That gives me this result:

结果是:

在数组中查找重复的值并输出它们

You can see that it picked up the duplicate score of 5 with players 2 & 4.

你可以看到,玩家2和4的重复分数是5。

#2


1  

Your code looks for duplication in consécutive values. Try this code to output duplicate values in an array.

您的代码在连续的值中查找重复。尝试此代码在数组中输出重复的值。

  for (column = 0; column < WinningScores.Length -1 ; column++) 
     {

        for (int cl= column + 1 ; cl < WinningScores.Length - 1 ; cl++)
            {
               if (WinningScores[column] == WinningScores[cl]) {


                        duplicateScore = WinningScores[column];
                        duplicateIndex = column;
                        Console.Write("\n Competitor {0} is the Winner with a total  of: {1}", duplicateIndex + 1, duplicateScore - totalSum);
              }
             }
         }

#3


0  

//Starts loop through first element
for(int i = 0; i < arr.length, i++)

//starts at second element in array
for(int j = i + 1; k < arr.length, j++)

//double check my logic though, in a hurry at work so had to post this in a rush.