-1

I have a problem with finding second largest number in my array.The thing is, it doesn't work for all my examples. After i read numbers from keyboard i put them in method test and then i sort them.. example:(user entered) 1,2,3,4,5,6,7,8,9,10 exit:10,9,..3,2,1 Now i want to display second largest number with for loop.. **The task is: find second largest number if its possible, if not CW("error") **

in code Comment //HERE i'm not sure how to writhe correct that part of code.

i hope my problem makes sense...

    public static int test(int[] polje)
    {

        int temp = 0;
        Console.WriteLine();
        for (int c = 0; c < polje.Length; c++)
        {
            for (int b = c + 1; b < polje.Length; b++)
            {
                if (polje[c] > polje[b])
                {
                    temp = polje[c];
                    polje[c] = polje[b];
                    polje[b] = temp;

                }

            }

        }

        int secondlargest = 0;



        //HERE

        for (int i = polje.Length - 1; i >= 0; i--)
        {
            if (polje[polje.Length - 2] == polje[polje.Length - 1] || polje[polje.Length - 2] == 0)
            {
                Console.WriteLine("Wrong!");
                break;
            }
            else
            {
                Console.WriteLine("Second largest number is :{0}", polje[polje.Length - 2]);
                secondlargest = polje[polje.Length - 2];
                break;
            }
        }


        return secondlargest;

    }
    static void Main(string[] args)
    {

        int[] polje = new int[10];
        Console.WriteLine("Enter values");
        for (int i = 0; i < 10; i = i + 1)
        {

            polje[i] = int.Parse(Console.ReadLine());
            if (polje[i] == 0)
            {
                break;
            }
        }
        test(polje);

        Console.ReadLine();
    }
}

}

  • 1
    Is this homework? Because you use a List and sort, or LINQ and OrderByDescending and Take(2) to get the two largest values. – Panagiotis Kanavos Nov 29 '16 at 17:50
  • If you're sorting them already, what's the point of the second for loop? Depending on whether you're sorting in ascending or descending order, as long as your array has more than two records, you can just select the second index of the array or the second last index of the array. – RizJa Nov 29 '16 at 17:50
  • 3
    http://stackoverflow.com/questions/14810444/find-the-second-maximum-number-in-an-array-with-the-smallest-complexity – Damith Nov 29 '16 at 17:50
  • it is for my homework.. the thing is i have to avoid some entry numbers, if that makes sense.. example: (input) 1, 2, 2, 0 output: second largest is 1. (input 2) 2, 1, 1, 0 output 2: second largest is 1. – Marko Škrilec Nov 29 '16 at 18:01
  • You mean you have to avoid duplicate entries. – Corey Berigan Nov 29 '16 at 18:33
  • @CoreyBerigan correct. – Marko Škrilec Nov 29 '16 at 18:45

2 Answers2

4

It is very simple:

var secondMaxValue = yourArray.OrderByDescending(x=> x).Skip(1).FirstOrDefault();
Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57
  • You should probably mention that `using System.Linq;` is required – Grax32 Nov 29 '16 at 17:54
  • 1
    If there are two same numbers and you don't want that you can use Distinct(). However, it is not explained in the question what exactly is required in that case. – Ivan Ičin Nov 29 '16 at 17:55
  • We cannot use anything from .net or other advanced features. – Marko Škrilec Nov 29 '16 at 17:58
  • 2
    @MarkoŠkrilec If you cannot use anything from .net then you cannot use C#. – juharr Nov 29 '16 at 18:21
  • @juharr its for my homework.. we cannot use all functions I'm really pissed because off that. Teacher told me i can do it with normal array sorting and with one for loop that goes over array[] backwards adding if condition and that's it. – Marko Škrilec Nov 29 '16 at 18:26
  • @MarkoŠkrilec Just set a variable to the last value then loop from the end until you see a value that is not equal to that one and that will be the second largest. That or you'll loop through the entire array and all the values are the same value. – juharr Nov 29 '16 at 18:30
  • @grax it could be that it's an answer to a low-quality, duplicate question which [already has an answer](http://stackoverflow.com/questions/14810444/find-the-second-maximum-number-in-an-array-with-the-smallest-complexity). Someone may be trying to discourage answering such questions in lieu of closing as a duplicate. Maybe they recognized that this is a homework problem meant to familiarize students with interacting with arrays on a basic (non-LINQ) level. Who knows? That said, this is precisely the solution I though of when I read the question. _*shrug*_ – canon Nov 29 '16 at 18:31
  • @Grax While true, that also shows up as a suggestion if you don't have it (and all C# files do when created). – BradleyDotNET Nov 29 '16 at 18:39
  • wow, sweet, short and simplest. – N Khan Apr 05 '18 at 08:36
0
int GetSecondLargest(int[] a){
    int a0,b0;
    for(int i = 0; i <a.Length;i++){
            if(a[i] > a0){ 
                  b0 = a0;
                  a0 = a[i];
            }else if(a[i] > b0) b0 = a[i];
   }
   return b0;

Edit: formatting is terrible, but the idea is that you keep the largest value and get the one right below it instead using basic conditionals. No Linq needed.