top of page

How to Find the 2nd Highest number in array.

  • Writer: prashant raj
    prashant raj
  • Jan 28, 2022
  • 1 min read


// Example program
#include<iostream>
using namespace std;

int getMaxValue(int *arr,int length)
{
    int max = arr[0];
    int max1 = arr[0];
    for(int i =1; i<length;i++)
    {
        if(arr[i] > max)
        {
            max = arr[i];
        }
    }
    
    for(int i =1; i<length;i++)
    {
        if(arr[i] > max1 && arr[i] != max)
        {
            max1 = arr[i];
        }
    }
    
    return max1;
}
int main()
{
    int arr[] = {10,4,18,9,55,85,7};
    int len = sizeof(arr)/sizeof(arr[0]);
    int value = getMaxValue(arr,len);
    cout<<value<<endl;
    
}

Comments


Drop Me a Line, Let Me Know What You Think

Thanks for submitting!

© 2023 by Train of Thoughts. Proudly created with Wix.com

bottom of page