Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Given an arraylist of strings. Write a method to sort the arraylist alphabetically and display the sorted elements with the number of times they occurred
Given an arraylist of strings. Write a method to sort the arraylist alphabetically and display the sorted elements with the number of times they occurred in the arrayList (don't print the same element twice). Is there a way to do this without using hash maps? Say I want to use insertion sort. say arraylist is: ("b", "a", "c", "c", "a", "b", "b") output should be: a 2 b 3 c 2 here what I have so far for insertion sort.
public static> void sortAlphabetically(ArrayList arrList) { for(int i = 1; i < arrList.size(); i++) { AnyType temp = arrList.get(i); int j = i; for( ; j > 0 && temp.compareTo(arrList.get(j-1)) < 0; j--) { arrList.set(j, arrList.get(j-1)); } arrList.set(j, temp); } } Now, I just need some way to keep track of the number of duplicates and display the elements (only once each if they are duplicated). I probably will need a new array list.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started