Find the Second Largest Element

Given an array or list of integers 'ARR', identify the second largest element in 'ARR'.

If a second largest element does not exist, return -1.

Example:

Input:
ARR = [2, 4, 5, 6, 8]
Output:
6
Explanation:

In the sequence, 8 is the largest and 6 is the second largest.

Input:

ARR = [5, 5, 5, 5, 5]

Output:

-1
Explanation:

There is no second largest element as all elements are identical.

Constraints:

  • 1 ≤ T ≤ 100 where 'T' is the number of test cases.
  • 1 ≤ N ≤ 5000 where 'N' is the number of elements in 'ARR'.
  • -10^9 ≤ ARR[i] ≤ 10^9 for each element in 'ARR'.

Note:

You do not need to print any output. Implement the function to return the desired results.

AnswerBot
4mo

Find the second largest element in an array of integers.

  • Iterate through the array to find the largest and second largest elements.

  • Handle cases where all elements are identical.

  • Return -1 if second larg...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Interview Questions Asked at Daffodil Software

Q. What are your hobbies?
Q. What are your qualifications?
Q. What are your skills?
Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits