Print the largest and second largest elements in an array without sorting

using System;

using System.Collections.Generic;

using System.Text;

using System.Collections;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            int[] numArray = { 11, 16, 11, 18, 12, 12, 18, 14, 13, 17, 19, 19};

            //Print the largest and second largest elements in an array without sorting

            if (numArray.Length > 1)

            {

                int largest = 0;

                int secondLargest = 0;

                if (numArray[0] >= numArray[1])

                {

                    largest = numArray[0];

                    secondLargest = numArray[1];

                }

                else

                {

                    largest = numArray[1];

                    secondLargest = numArray[0];

                }

                for (int i = 2; i < numArray.Length; i++)

                {

                    if (numArray[i] > largest)

                    {

                        secondLargest = largest;

                        largest = numArray[i];

                    }

                    else if (numArray[i] > secondLargest && numArray[i] != largest)

                    {

                        secondLargest = numArray[i];

                    }

                    else if (largest == secondLargest && numArray[i] < secondLargest)

                    {

                        secondLargest = numArray[i];

                    }

                }

                if (largest == secondLargest)

                {

                    Console.WriteLine("No second largest element. Largest element is: " + largest);

                }

                else

                {

                    Console.WriteLine("Largest element is: " + largest);

                    Console.WriteLine("Second largest element is: " + secondLargest);

                }

            }

            else

            {

                Console.WriteLine("Not enough elements in the array.");

            }

        }

    }

}
 
Largest element is: 19
Second largest element is: 18
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

One Response to Print the largest and second largest elements in an array without sorting

  1. Saikat says:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication4
    {
    class Program
    {
    static void Main(string[] args)
    {
    //Largest and second largest element in an array
    int[] numArray = { 5, 2, 7, 10, 21, 4, 3, 71, 70 };
    if (numArray.Length >= 2)
    {
    int largest = 0, secondLargest = 0;
    for (int i = 0; i < numArray.Length; i++)
    {
    if (i == 0)
    {
    largest = numArray[i];
    }
    else if (i == 1)
    {
    if (numArray[i] largest)
    {
    int temp = largest;
    largest = numArray[i];
    secondLargest = temp;
    }
    else if (numArray[i] > secondLargest)
    {
    secondLargest = numArray[i];
    }
    }
    }
    Console.WriteLine("Largest = " + largest);
    Console.WriteLine("Second Largest = " + secondLargest);
    }
    else
    {
    Console.WriteLine("There are fewer elements in the array than expected");
    }
    }
    }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s