import
java.util.*;
import
java.util.stream.*;
class
GFG {
static
List<Integer> sortDesc(
int
[] arr)
{
Map<Integer, Integer> d_sum =
new
HashMap<>();
Map<Integer, Integer> d_count =
new
HashMap<>();
List<Integer> ans =
new
ArrayList<>();
int
mx =
0
;
for
(
int
i =
0
; i < arr.length; i++) {
int
x = arr[i];
if
(d_sum.containsKey(x)) {
d_sum.put(x, d_sum.get(x) + x);
d_count.put(x, d_count.get(x) +
1
);
}
else
{
d_sum.put(x, x);
d_count.put(x,
1
);
}
}
List<Map.Entry<Integer, Integer> > l
=
new
ArrayList<>(d_sum.entrySet());
Collections.sort(l, (e1, e2) -> {
int
cmp
= e2.getValue().compareTo(e1.getValue());
if
(cmp ==
0
) {
cmp = e1.getKey().compareTo(e2.getKey());
}
return
cmp;
});
for
(Map.Entry<Integer, Integer> e : l) {
int
x = e.getKey();
int
count = d_count.get(x);
while
(count >
0
) {
ans.add(x);
count--;
}
}
return
ans;
}
public
static
void
main(String[] args)
{
int
[] arr = {
3
,
5
,
2
,
2
,
3
,
1
,
3
,
1
};
List<Integer> sortedList = sortDesc(arr);
System.out.println(
sortedList.stream()
.map(Object::toString)
.collect(Collectors.joining(
" "
)));
}
}