Right now I have something in php that looks like this:
$count = array_count_values($result);
arsort($count);
foreach($count as $key => $val){
$result[] = $key;
}
It will count all the items in the array and put it into a key/value pair. Which will remove the duplicates and then I tell it to sort. Then I take the key of it and store it. Is there a way to do this in Java?
I don’t believe Java has an equivalent to the
array_count_valuesfunction, so you will need to implement that yourself. Something like this:Then use the
java.util.Collections.sort(List list, Comparator c)function to sort the array by the counts. You’ll need to implement Comparator to sort by the counts.