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
1y
The task is to find the second largest element in an array of integers.
Iterate through the array and keep track of the largest and second largest elements.
Initialize the largest and second largest var...read more
Prachi Singh
9mo
li.stream().distinct() .sorted(Comparator.reverseOrder()).skip(1).findFirst() .ifPresentOrElse( value -> System.out.println("Second largest element: " + value), () -> System.out.println("-1") );
Anonymous
2y
asuming element (4 6 9 3)
here 2nd largest element is 6
Add answer anonymously...
TCS Software Engineer interview questions & answers
A Software Engineer was asked 1d agoQ. What is DevOps?
A Software Engineer was asked 2mo agoQ. What are the SOLID design principles?
A Software Engineer was asked 2mo agoQ. What is a data type?
Popular interview questions of Software Engineer
A Software Engineer was asked 1d agoQ1. What is DevOps?
A Software Engineer was asked 1d agoQ2. What is GenAI?
A Software Engineer was asked 2mo agoQ3. What are the SOLID design principles?
Top HR questions asked in TCS Software Engineer
A Software Engineer was asked 2mo agoQ1. Can you explain the project that excited you?
A Software Engineer was asked 2mo agoQ2. Why do you want to join this job?
A Software Engineer was asked 3mo agoQ3. How does the role fit your career plans?
Stay ahead in your career. Get AmbitionBox app


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
AmbitionBox Awards
Get AmbitionBox app

