Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!