Question: Write a function how to find the second largest element of an array or list without any…
You are in an 15 min interview for a frontend role and you are being asked to find the second largest number from a give list, how would…
Question: Write a function how to find the second largest element of an array or list without any inbuilt function? How to conquer this.
You are in an 15 min interview for a frontend role and you are being asked to find the second largest number from a give list, how would you find this, lets deep down into that.
Question: Write a function how to find the second largest element of an array or list without any inbuilt function?
Anwer:
int findSecondLargestNumber( arr){
if(arr.length < 2) return null;
let largest = arr[0];
let secondLargestNumber = -Infinity;
for(let i =0; i < arr.length; i++){
if(arr[i]>largest){
secondLargestNumber = largest;
largest = arr[i]
}
else if(arr[i]> secondLargestNumber && arr[i] !== largest){
secondLargestNumber = arr[i];
}
}
return(secondLargestNumber);
}
console.log(findSecondLargestNumber([39, 22, 45, 34, 10]);
Please comment if you have better approach to find the second smallest number.
메타데이터
- post_id
- 866f79f14e3f
- slug
- question-write-a-function-how-to-find-the-second-largest-element-of-an-array-or-list-without-any-866f79f14e3f
- url
- https://medium.com/@shubnihal/question-write-a-function-how-to-find-the-second-largest-element-of-an-array-or-list-without-any-866f79f14e3f
- canonical_url
- https://medium.com/@shubnihal/question-write-a-function-how-to-find-the-second-largest-element-of-an-array-or-list-without-any-866f79f14e3f
- author_url
- https://medium.com/@shubnihal
- status
- ok
- fetched_at
- 2026-06-09 15:37:30