Skip to content

Instantly share code, notes, and snippets.

@rabestro
Last active December 27, 2020 11:27
Show Gist options
  • Save rabestro/134c017bbeeee742160d4f30fdbf7a09 to your computer and use it in GitHub Desktop.
Save rabestro/134c017bbeeee742160d4f30fdbf7a09 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static int findSecondLargestNumber(int[] numbers) {
// write your code here
return Arrays.stream(numbers)
.distinct()
.map(i -> -i)
.sorted()
.skip(1)
.limit(1)
.map(i -> -i)
.findFirst()
.orElse(Integer.MIN_VALUE);
}
/* Do not change code below */
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
final int[] numbers;
if (scanner.hasNextInt()) {
numbers = Arrays.stream(scanner.nextLine().split("\\s+"))
.mapToInt(Integer::parseInt)
.toArray();
} else {
numbers = new int[0];
}
System.out.println(findSecondLargestNumber(numbers));
}
}
import java.util.Arrays;
import java.util.Scanner;
public class Main2 {
public static int findSecondLargestNumber(int[] numbers) {
int firstMax = Arrays.stream(numbers)
.max().orElse(Integer.MIN_VALUE);
int secondMax = Arrays.stream(numbers)
.filter(i -> i < firstMax)
.max().orElse(Integer.MIN_VALUE);
return secondMax;
}
/* Do not change code below */
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
final int[] numbers;
if (scanner.hasNextInt()) {
numbers = Arrays.stream(scanner.nextLine().split("\\s+"))
.mapToInt(Integer::parseInt)
.toArray();
} else {
numbers = new int[0];
}
System.out.println(findSecondLargestNumber(numbers));
}
}
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static int findSecondLargestNumber(int[] numbers) {
int firstMax = Integer.MIN_VALUE;
int secondMax = Integer.MIN_VALUE;
for (int number : numbers) {
if (number > firstMax) {
secondMax = firstMax;
firstMax = number;
} else if (number > secondMax && number != firstMax) {
secondMax = number;
}
}
return secondMax;
}
/* Do not change code below */
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
final int[] numbers;
if (scanner.hasNextInt()) {
numbers = Arrays.stream(scanner.nextLine().split("\\s+"))
.mapToInt(Integer::parseInt)
.toArray();
} else {
numbers = new int[0];
}
System.out.println(findSecondLargestNumber(numbers));
}
}

Find the second largest element in an array

Implement a method to find the second largest number in an array of ints.

If the input array is empty or contains only a single number, the method must return Integer.MIN_VALUE.

If the input array contains multiple largest elements, consider them as the same value.


Sample Input:
1 5 3 1 2 4 6

Sample Output:
5


Sample Input:
17 17 -10 -10 -15

Sample Output:
-10


Sample Input:
15 15

Sample Output:
-2147483648



Memory limit: 256 MB
Time limit: 2 seconds

Show topic summary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment