Nth largest and smallest

Run Settings
LanguageJava
Language Version
Run Command
import java.util.Arrays; import java.util.Scanner; import java.util.TreeSet; class Main { // if there is no duplicate // if there is duplicate use TreeSet public static void main(String[] args) { Scanner scan = new Scanner(System.in); // int a[] = {1,23,-9,12,34,5,67}; // Arrays.sort(a); // int n = scan.nextInt(); // System.out.println(a[n-1]); int a[] = {1,23,-9,12,34,5,67,-9,1}; int n = 3; TreeSet<Integer> set = new TreeSet<>(); for(int i: a) set.add(i); int i=1; for(int s: set) { if(i==n) {System.out.println(s); break;} i++; } } }
Editor Settings
Theme
Key bindings
Full width
Lines