Computer Science, asked by anindyaadhikari13, 8 months ago

Good afternoon friends.
Today's Question.
Write a program in Java to find the second largest element from an array without sorting.
Its not a challenge ✔✔
Solve it.
All the best.
Spammers stay away. ​

Answers

Answered by Oreki
2

public class SecondLargest {

static void printSecondLargest(int[ ] x) {

int first = x[0],

second = x[x.length - 1];

for (int i = 0; i < x.length; i++) {

if (first < x[i]) {

second = first;

first = x[i];

} else if (x[i] > second && x[i] != first)

second = x[i];

}

System.out.println((second = = first) ? "There is no second largest element" : "The Second largest element in the array is - " + second);

}

public static void main(String[ ] args) {

printSecondLargest(new int[ ] {12, 13, 14, 15, 17, 11});

printSecondLargest(new int[ ] {12, 12, 12, 12});

}

}

Similar questions