We have sorted the array first then second largest value would be arr[arr.length-2] directly.....No need to run for loop again and if condition also... If I am wrong please correct me .
Yes you can directly do it. But what if there is duplicate eg: 1,2,3,4,5,5....In this case if you directly return arr[are.length-2] it will return 5 which is wrong....So it's necessary to check if your arr[n-2] is equal to last element or not
Sir india students need and necessary for this type of lectures
@@malingaraj7180 please share
This type of simple question really asked in interview in today's time 😂😂
sir please continue coding question series for interview
sor plz teach bubble.sort selection sort etc in simple manner
We have sorted the array first then second largest value would be arr[arr.length-2] directly.....No need to run for loop again and if condition also...
If I am wrong please correct me .
Yes you can directly do it. But what if there is duplicate eg: 1,2,3,4,5,5....In this case if you directly return arr[are.length-2] it will return 5 which is wrong....So it's necessary to check if your arr[n-2] is equal to last element or not
To find 2nd largest element madhi error yetay sir please check it with different elements in array