Issue
I need to separate and count how many values in arraylist are the same and print them according to the number of occurrences.
I've got an arraylist called digits :
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]
I created a method which separates each value and saves it to a new array.
public static ArrayList<Integer> myNumbers(int z) {
ArrayList<Integer> digits = new ArrayList<Integer>();
String number = String.valueOf(z);
for (int a = 0; a < number.length(); a++) {
int j = Character.digit(number.charAt(a), 10);
digits.add(j);
}
return digits;
}
After this I've got a new array called numbers. I'm using sort on this array
Collections.sort(numbers);
and my ArrayList looks like this:
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9]
It has:
2 times 0;
9 times 1;
4 times 2;
6 times 3;
5 times 4;
6 times 5;
5 times 6;
5 times 7;
5 times 8;
3 times 9;
I need to print out the string of numbers depend on how many are they So it suppose to look like this : 1354678290
Solution
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("a");
list.add("a");
list.add("a");
int countA=Collections.frequency(list, "a");
int countB=Collections.frequency(list, "b");
int countC=Collections.frequency(list, "c");
Answered By - pruthwiraj.kadam
Answer Checked By - Candace Johnson (JavaFixing Volunteer)