compile Undo Cut Copy Paste-1. Find.. .11 Close Provided as input is labels, an
ID: 3601436 • Letter: C
Question
compile Undo Cut Copy Paste-1. Find.. .11 Close Provided as input is labels, an Arraylist of strang variables. Return the last string alphabetically in labels xithout using a loop. For exanple, if labels was Ibananas, grapes, strawberries apples then return stranberries. - For full credit on this problen, your code nust work , for any 1ist consisting of at least one iten, Do not , write your code to work sonly for the specific test cases s Run the test several times. It will try the sane words " but will alter the order of the words on each test run. private static String req5a(Arraylist String» labels) return null: delete this 1ine Sane problen as above but with two changes: 1 you cannot change the order of itess an labels + 2. return the index, of the last String alphabetically - Ex: if labels is Ibananas, grapes, stranberries, apples] then return 2 (the index of strawberries) For full credit on this probles, your code aust work : for any 11st consisting of at least one item. Do not " arste your code to work only for the specific test . Rur the test several ties. It will try the sano words but w11 alter the order of the words on each test run. private static int reasb(Arraylist Strings labels) return 9: delete this 11ne DO NOT EDIT ANY CODE BELON HEREExplanation / Answer
Please find my implementation.
private static String req5a(ArrayList<String> labels) {
if(labels == null || labels.size() == 0)
return null;
// sorting arraylist
Collections.sort(labels);
// returning last element
return labels.get(labels.size()-1);
}
private static int req5b(ArrayList<String> labels) {
if(labels == null || labels.size() == 0)
return -1;
// initializing max element index with first elemet
int maxIndex = 0;
String max = labels.get(0);
for(int i=1; i<labels.size(); i++) {
if(labels.get(i).compareTo(max) >= 0) {
max = labels.get(i);
maxIndex = i;
}
}
return maxIndex;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.