2

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.

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);
    }
}
14
  • 1
    Hey! Can you please tell us for some sample values what is the output that you are getting and what is expected? It would be easier for us to understand the issue. Commented Apr 4, 2016 at 12:40
  • If it's always one short is it something to do with WinningScores.Length -1 ... should it just be WinningScores.Length ? Commented Apr 4, 2016 at 12:41
  • its like its missing the last value of the Array "WinningScores" Commented Apr 4, 2016 at 12:42
  • 1
    @Diam In your If condition, that would allow you to make a different action for the last repetition only. However, you'll want to do something else, you're not checking for duplicates here but for two consecutive values Commented Apr 4, 2016 at 12:49
  • 1
    "I can't use system.array". You really should tell us why we might have to tie our hands behind our backs to solve your problems. Commented Apr 4, 2016 at 12:52

3 Answers 3

3

You could try using LINQ for this:

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:

result

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

Sign up to request clarification or add additional context in comments.

2 Comments

I wonder if OP can't use System.Array whether linq might fall into the same category.
@spender - probably, but he wasn't clear on that or why he couldn't use System.Array. It's still a nice answer and SO is for the community, not just the OP.
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);
              }
             }
         }

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.