Java Programs: How to Find the Second Largest Element #javaprogramming #interviewquestions

preview_player
Показать описание
Java Programs: How to Find the Second Largest Element | Java Interview Questions and Answers | Java Interview Question | Test Automation Central

Automation Testing Interview Question | Selenium WebDriver Java Interview Question | Automation Tutorials | Selenium Tutorials | Testing Automation Tutorials | Test Automation | Java Selenium | Automation Interview Question And Answers | ChromeDriver Class | WebDriver Interface

#sdet #seleniumtutorial #automationtester #automationtesting #javaprogramming #javatutorial #javaprogram #javaquestions #testautomation #testautomationcentral #seleniumtutorialforbeginners #seleniumwebdriver #javaselenium #seleniuminterview #javaprogram #javaprogramming #javaarrays #javaobjects #recursion #javarecursion #anagrams #javaanagrams #javaarraylist
Рекомендации по теме
Комментарии
Автор

this will run for duplicates arr elements also


public class Practice{
public static void main(String arr[]){
int ar[]={50, 50, 32, 4};
int max=Integer.MIN_VALUE;
int
for(int i=0;i<ar.length;i++){
if(ar[i]>max){
secondLargest=max;
max=ar[i];

}
else if(ar[i]>secondLargest && ar[i]!=max){
secondLargest=ar[i];
}

}



}
}

kundankumargoswami
Автор

Sort the array then run loop from right any element smaller than the largest will be second smallest element

anubhav.codess
Автор

If the array element is duplicated then it will give wrong value.

Eg [6, 7, 7, 8, 8]

praveenrajdhandapani
Автор

Sort the array then retrieve the second element at the end of the array by subtracting the array length by 2

iamnjabulo.
Автор

sir it is failed for n=5
array elements (90 10 20 -5 50)
correct is first declare second with minimum number
second

ramrathore
Автор

Arrays
.stream(arr)
.boxed()

.skip(1)
.findFirst()
.get();

arpanganguly
Автор

Integer[] arr = {5, 6, 88, 2, 36, 95};
List<Integer> list = Arrays.asList(arr);

Collections.sort(list, Collections.reverseOrder());

if (list.size() < 2) {
throw new should have at least 2 elements.");
}

int secondLargest = list.get(1);
System.out.println("Second largest element: " + secondLargest); //88
}
}

muhammedakcal
join shbcf.ru