Write a program to sort given array according to frequency of occurrence of numbers, in descending order.
Input (int[]) | Output (int[]) |
---|---|
{1, 4, 3, 4, 5, 4, 5} | {4, 4, 4, 5, 5, 1, 3} |
{9, 3, 9, 2, 3, 2, 1, 2, 2, 3} | {2, 2, 2, 2, 3, 3, 3, 9, 9, 1} |
{4, -1, 0, 4, -1, 0, -1} | {-1, -1, -1, 4, 4, 0, 0} |
{6, 4, 6, 1, 6, 1} | {6, 6, 6, 1, 1, 4} |
class SortByOccurrenceInDescendingOrder
{ public static void main(String s[])
{
int array[] = {3, 4, 5, 0, 0, 0, 0, 0, 3, 3, 3, 5, 1, 5};
sortByOccurrence(array);
System.out.print("After sorting: ");
for (int element : array)
System.out.print(element + " ");
}
public static void sortByOccurrence(int[] inputArray) {
//Write code here to sort array according to occurrence of numbers in descending order
}
//If required, write any additional methods here
}
Topic: Learn Arrays And Loops