Program-find the nth highest number in an integer array
.Function to find the nth highest number in an integer array
import java.io.*;
import java.util.*;
public class highestNumber {
public static void main(String args[]) {
ArrayList<Integer> input = new ArrayList<Integer>();
input.add(1);
input.add(3);
input.add(8);
input.add(2);
input.add(4);
System.out.println(nthHighest(input,3));
}
public static int nthHighest(ArrayList<Integer> input, int n) {
Collections.sort(input);
ArrayList<Integer> temp = new ArrayList<>();
for(int j = input.size() - 1; j>=0; j--) {
temp.add(input.get(j));
}
// System.out.println(temp);
return temp.get(n - 1);
}
}
import java.io.*;
import java.util.*;
public class highestNumber {
public static void main(String args[]) {
ArrayList<Integer> input = new ArrayList<Integer>();
input.add(1);
input.add(3);
input.add(8);
input.add(2);
input.add(4);
System.out.println(nthHighest(input,3));
}
public static int nthHighest(ArrayList<Integer> input, int n) {
Collections.sort(input);
ArrayList<Integer> temp = new ArrayList<>();
for(int j = input.size() - 1; j>=0; j--) {
temp.add(input.get(j));
}
// System.out.println(temp);
return temp.get(n - 1);
}
}
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment