0

Learning C# and i'm trying to find the second largest array in a user-inserted list. I found a solution online:

int second = int.MinValue;
foreach (int i in playerScore)
{
    if (i > largest)
    {
        second = largest;
        largest = i;
    }
    else if (i > second)
        second = i;
}

But the problem is, if the two largest number are the same the loop still spit out the largest number. I can't think of a way to edit this loop so it finds the true second highest number in my array. Would a loop like this even be the most efficient way of doing this?

Thanks

0

10 Answers 10

2

Just change

else if (i > second)

to

else if (i > second && i != largest)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! If I wanted to find the third largest number could I try add in another else if statement? I tried messing around with it but i cant seem to get it to work correctly
1

The easiest algorithm (to be sure that everything is ok):

  1. Sort your array
  2. Loop through the max value until you find a smaller one.

But let's check your loop to make it working. The problem is in line else if (i > second) because if i==largest it is true. So let's change it to else if (i > second && i<largest)

The full code will be:

int second = int.MinValue;
foreach (int i in playerScore)
{
    if (i > largest)
    {
        second = largest;
        largest = i;
    }
    else if (i > second && i<largest)
        second = i;
}

Comments

1

You can use linq like this

var secondHighest = playerScore.Distinct().OrderByDescending(a => a).Skip(1).First();

Comments

0

You can do it with LINQ:

    int second = playerScore.Distinct().OrderByDescending(x => x).Skip(1).First();

Comments

0

You should just add add another condition to it:

if(i == largest) {
   continue;
}

Or if you want to get started with LINQ:

playerScore.Distinct().OrderByDescending(a => a).Take(2);

This will get you the biggest and 2nd biggest values.

Comments

0

You could try this: make an auxiliar collection that receives the original one, sort it decreasingly (there is many built-in sorting functions) and retrieve the 2nd element. Hope that works for you.

Comments

0

If you want to get the second biggest in the list, you can use LINQ (as many suggested already). Since you want to have the second biggest number (independent of how many times the biggest number occures) you can do it like that:

// sort decending: from the biggest to the smallest
var sorted = y.OrderByDescending(x => x);
// take the first element (which is the biggest)
var largest = sorted.First();
// exclude the biggest element from the list and take the first one now (which will be the second biggest)
var secondLargest = sorted.Where(x => x != largest).First();

Comments

0

Another way is using LINQ which i often prefer to increase readability.

But the problem is, if the two largest number are the same the loop still spit out the largest number

Then you could order the array and group by the number:

var secondGroup = playerScore
    .OrderByDescending(i => i)    // orders highest first
    .GroupBy(i => i)              // builds groups of unique numbers
    .Skip(1)                      // you don't want the highest
    .FirstOrDefault();            // get the second highest, if there is a second
if (secondGroup != null)
{
    int second = secondGroup.Key;
}

This handles the cases that there are less than two numbers or that the highest number is not unique.

Comments

0
int[] a=new int{1,2,3,4};
int firstbig=a[0];
int secondbig=a[1];
int p;
for(int i=0;i<a.length;i++)
 {
   if(firstbig<a[i])
     {
       firstbig=a[i];
       p=i;
     }
 }
for(int j=0;j<a.length;j++)
 {
  if(secondbig<a[j] && j!=p)
    {
     secondbig=a[j];

    }
 }

Comments

-1

I like this simple solution. No LINQ required:

public static int FindSecondHighest(int [] ints)
{
    // assumptions:
    // minimum two ints in the array
    // they're not all equal
    // they're not negative

    var highest = 0;
    var secondHighest = 0;

    foreach (int x in ints)
    {
        if (x >= highest)
        {
            secondHighest = highest;
            highest = x;
        }
    }

    return secondHighest;
}

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.