challenging java question) why doesn\'t this code work how can I do it right? st
ID: 3859448 • Letter: C
Question
challenging java question) why doesn't this code work how can I do it right?
static int Q4(ArrayList<Integer> input, int k){
// return the kth largest value in the input ArrayList ignoring duplicate values.
// Example: if input=[9,8,5,2,8] and k=3 the return value is 5 since the duplicate 8 is ignored
List<Integer> list = Arrays.asList(input);
Set<Integer> set = new TreeSet<Integer>(list);
List = new ArrayList<Integer>(set);
int value = (list.size()-1) - k;
return list.get(value);
}
Explanation / Answer
//Check Now, I have removed all the Complication Errors and sorted the list also, check it out
static int Q4(ArrayList<Integer> input, int k){
// return the kth largest value in the input ArrayList ignoring duplicate values.
// Example: if input=[9,8,5,2,8] and k=3 the return value is 5 since the duplicate 8 is ignored
Set<Integer> set = new TreeSet<Integer>(input);
List<Integer> l= new ArrayList<Integer>(set);
int value = (l.size()-1) - k;
Collections.sort(l);
return l.get(value);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.