DEV Community

Kushal
Kushal

Posted on

[3] Step by Step solution | Find the Second Largest element in array using C# | Java

So, the problem statement is we have to find out the second largest element in the array.

To, start you can always start with dividing the problem into pieces. So, you can think of the way how to find the largest element first.

So, suppose you have a array, {10, 1, 2, 33, 12} So the second largest element inside the array is 12.

How should you proceed, don't worry in the beginning how many loops you have to use, Just try to think how you should start on this problem.

If we take the first element, ie 10, then that will be the largest, then when we move forward and pick 1. 1 will be the second largest.

What we are doing in last para is, we are basically traversing the elements. Means, we need one array here.


            int largest = int.MinValue;

            for(int i =0; i < arrayLength; i++)
            {
                if(arr[i] > largest)
                {
                    largest = arr[i];
                }
            }
Enter fullscreen mode Exit fullscreen mode

What we just did is, we checked if our arr[i] is greater then largest, if that context element is largest then that will become the largest.

Let's extend the program, take 1. This 1 will become the second largest, that means something like this:


            int largest = int.MinValue;
            int secondlargest = int.MinValue;
            for(int i =0; i < arrayLength; i++)
            {
                if(arr[i] > largest)
                {
                    largest = arr[i];
                }
                else { 
                    secondlargest = arr[i];
                }
            }
Enter fullscreen mode Exit fullscreen mode

Now, pick 2 from the array, this 2 is greater then 1 and smaller then 10. we have to put this condition now in the code. So, this is the situation of else. in else if, we have to put this logic, let's put it..


            int largest = int.MinValue;
            int secondlargest = int.MinValue;
            for(int i =0; i < arrayLength; i++)
            {
                if(arr[i] > largest)
                {
                    largest = arr[i];
                }
                else if(arr[i] < largest && arr[i] > secondlargest) { 
                    secondlargest = arr[i];
                }
            }
Enter fullscreen mode Exit fullscreen mode

Now, let's pick 33, now 33 is greater then largest number. So, we need to put 33 in largest, and my largest will become the second largest. sounds logical!! Let's put last piece of code.


            int largest = int.MinValue;
            int secondlargest = int.MinValue;
            for(int i =0; i < arrayLength; i++)
            {
                if(arr[i] > largest)
                {
                    secondlargest = largest;
                    largest = arr[i];
                }
                else if(arr[i] < largest && arr[i] > secondlargest) { 
                    secondlargest = arr[i];
                }
            }
Enter fullscreen mode Exit fullscreen mode

Now, our code is complete. Hope you liked this journey. Video available at https://www.youtube.com/watch?v=angBdWeAxd8&t=205s

Git Links:
C#: https://github.com/kushalseth/DataStructure/blob/master/CodingProblems/CodingProblems/ArrayProblems/SecondLargestInArray.cs

Java: https://github.com/kushalseth/DataStructure/blob/master/javadatastructure/src/Main.java

Stay Connected for more informative stuff on React, Javascript, Algorithms, Blockchain, DataStructures, etc

Top comments (0)