Find the largest and second largest element in an array
The problem at hand is to find the largest and secondlargest elements in an array of integers. This task involves iterating through the array and identifying the two largest values present in it. This can be helpful in various scenarios, such as finding the topscoring students in a class or the highest and secondhighest sales figures in a dataset.
Problem Statement and Description
Given an array of integers, we need to determine the largest and secondlargest values present in the array. For
instance, consider the array [10, 3, 23, 86, 8, 9, 44, 5, 7]
. The largest element in this array is 86,
and the secondlargest element is 44.
Examples
Example A
arr1[] = {10 , 3 , 23 , 86 , 8 , 9 , 44 , 5 , 7}
⇧ ⇧

First Largest : 86
Second Largest : 44
Example B
arr2[] = {10 , 3 , 11 , 16 , 8 , 16 , 5 , 9 , 7}
⇧ ⇧

First Largest : 16
Second Largest : 16
Example C
arr3[] = { 10 }

None
(Because less than 2 element)
Idea to Solve the Problem
To find the largest and secondlargest elements in the array, we can iterate through the array while keeping track
of the two largest values encountered so far. We'll initialize two variables, first
and
second
, to store these values. As we traverse the array, we'll compare each element with these
variables and update them accordingly. By the end of the iteration, we'll have the largest and secondlargest
elements.
Pseudocode
twoLargest(arr, size):
if size <= 1:
return
else:
first = INT_MIN
second = INT_MIN
for i in range(0, size):
if arr[i] > first:
second = first
first = arr[i]
else if arr[i] > second:
second = arr[i]
print "First Largest: ", first
print "Second Largest: ", second
Algorithm Explanation
 Check if the size of the array is less than or equal to 1. If so, there can't be a secondlargest element, and we return.
 Initialize two variables,
first
andsecond
, toINT_MIN
. These will be used to store the largest and secondlargest elements.  Iterate through the array. For each element
arr[i]
: Compare
arr[i]
withfirst
. If it's greater thanfirst
, updatesecond
with the value offirst
and updatefirst
with the value ofarr[i]
.  If
arr[i]
is not greater thanfirst
but is greater thansecond
, updatesecond
with the value ofarr[i]
.
 Compare
 After the iteration, the
first
variable will contain the largest element, and thesecond
variable will contain the secondlargest element.  Print the values of
first
andsecond
.
Code Solution

1) Find the largest and second largest element of array in java
2) Find the largest and second largest element of array in c++
3) Find the largest and second largest element of array in c#
4) Find the largest and second largest element of array in c
5) Find the largest and second largest element of array in vb.net
6) Find the largest and second largest element in an array in node js
7) Find the largest and second largest element of array in scala
8) Find the largest and second largest element of array in swift
9) Find the largest and second largest element of array in kotlin
10) Find the largest and second largest element in an list in python
11) Find the largest and second largest element in an array in ruby
12) Find the largest and second largest element in an array in golang
13) Find the largest and second largest element of array in typescript
14) Find the largest and second largest element of array in php
Time Complexity Analysis
The algorithm iterates through the array once, performing constanttime comparisons and updates for each element. Therefore, the time complexity of the algorithm is O(n), where n is the size of the input array. This linear time complexity indicates that the algorithm's execution time increases linearly with the size of the input.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment