# C Program To Sort an array by frequency

Sorting an array based on its frequency means the element will be sorted in the decreasing order of their frequency. Here, we are given an array and our task is to sort the array based on their frequency.

But before moving forward if you are not familiar with the concept of the array in C, then do check the article on Arrays in C.

**Input:** Enter the Array: 2 4 3 5 4 2 3 1 2

**Output:** Sorted Array by Frequency: 2 2 2 4 4 3 3 5 1

Explanation: Here, 2 occurred 3 times, 4 occurred 4 times, 3 occurred 3 times, 5 occurred only once and 1 also occurred only once.

## Program 1: Sort an Array By Frequency

In this method, in the main method itself, we declare the array and count the frequency of each element. Then, we sort the array elements based on their frequency. In the end, display the resultant array.

### Algorithm

- Start
- Declare two arrays.
- Initialize the first array.
- Calculate the frequency of each element and then store it in the frequency array.
- Display the array element and its corresponding frequency.
- Now sort the frequency array and display the result along with the corresponding array element.
- Stop.

In the below program, firstly we declare an array and then in the main program itself, we find the frequency of each element. Then we store the frequency of each unique element in the new array. After that, we display the sorted array based on their frequency.

```
#include <stdio.h>
#define MAX 256
int main ()
{
int arr[MAX][2], brr[MAX][2];
int k = 0, n, temp, count;
//Initialize array size
printf ("\nEnter the number of elements:\n");
scanf ("%d", &n);
//Initialize array elements
printf ("\nEnter the array elements :\n");
for (int i = 0; i < n; i++)
{
scanf ("%d", &arr[i][0]);
arr[i][1] = 0;
}
// Unique elements and its frequency are stored in another array
for (int i = 0; i < n; i++)
{
if (arr[i][1])
continue;
count = 1;
for (int j = i + 1; j < n; j++)
{
if (arr[i][0] == arr[j][0])
{
arr[j][1] = 1;
count++;
}
}
brr[k][0] = arr[i][0];
brr[k][1] = count;
k++;
}
n = k;
//Print the elements and its frequency
printf ("\nArray Elements and its frequency:\n");
printf (" \nElements Frequency\n");
for (int i = 0; i < n; i++)
{
printf (" %d %d \n", brr[i][0], brr[i][1]);
}
//Store the array and its frequency in sorted form
for (int i = 0; i < n - 1; i++)
{
temp = brr[i][1];
for (int j = i + 1; j < n; j++)
{
if (temp < brr[j][1])
{
temp = brr[j][1];
brr[j][1] = brr[i][1];
brr[i][1] = temp;
temp = brr[j][0];
brr[j][0] = brr[i][0];
brr[i][0] = temp;
}
}
}
//Print the sorted array and its frequency
printf ("\nSorted Array Elements based on their frequency:\n");
printf (" Elements Frequency\n");
for (int i = 0; i < n; i++)
{
printf (" %d %d \n", brr[i][0], brr[i][1]);
}
//Print the sorted array based on its frequency
printf ("\n Sorted Array based on its frequency:\n");
for (int i = 0; i < n; i++)
{
while (brr[i][1] != 0)
{
printf (" %d ", brr[i][0]);
brr[i][1]--;
}
}
return 0;
}
```

Enter the number of elements: 10

Enter the array elements : 2 4 3 2 1 8 3 3 7 2

Array Elements and its frequency:

Elements Frequency

2 3

4 1

3 3

1 1

8 1

7 1

Sorted Array Elements based on their frequency:

Elements Frequency

2 3

3 3

4 1

1 1

8 1

7 1

Sorted Array based on its frequency:

2 2 2 3 3 3 4 1 8 7

## Program 2: Sort an Array By Frequency

In this method, we will call a function that will perform actions like count the frequency of each element in the array, then sort the array based on their frequency. After sorting the array display the resultant array.

### Algorithm

- Start
- Declare two arrays.
- Initialize the first array.
- Call a function that will sort the array based on its frequency.
- Calculate the frequency of each element and then store it in the frequency array.
- Display the array element and its corresponding frequency.
- Now sort the frequency array and display the result along with the corresponding array element.
- Stop.

Below is the code for the same.

In the below program, firstly we declare an array and then call a function to find the frequency of each element. Then we store the frequency of each unique element in the new array. After that, we display the sorted array based on their frequency.

```
#include <stdio.h>
#define MAX 256
void sortArray (int arr[MAX][2], int n, int brr[MAX][2])
{
int k = 0, temp, count;
// Unique elements and its frequency are stored in another array
for (int i = 0; i < n; i++)
{
if (arr[i][1])
continue;
count = 1;
for (int j = i + 1; j < n; j++)
{
if (arr[i][0] == arr[j][0])
{
arr[j][1] = 1;
count++;
}
}
brr[k][0] = arr[i][0];
brr[k][1] = count;
k++;
}
n = k;
//Print the elements and its frequency
printf ("\nArray Elements and its frequency:\n");
printf (" \nElements Frequency\n");
for (int i = 0; i < n; i++)
{
printf (" %d %d \n", brr[i][0], brr[i][1]);
}
//Store the array and its frequency in sorted form
for (int i = 0; i < n - 1; i++)
{
temp = brr[i][1];
for (int j = i + 1; j < n; j++)
{
if (temp < brr[j][1])
{
temp = brr[j][1];
brr[j][1] = brr[i][1];
brr[i][1] = temp;
temp = brr[j][0];
brr[j][0] = brr[i][0];
brr[i][0] = temp;
}
}
}
//Print the sorted array and its frequency
printf ("\nSorted Array Elements based on their frequency:\n");
printf (" Elements Frequency\n");
for (int i = 0; i < n; i++)
{
printf (" %d %d \n", brr[i][0], brr[i][1]);
}
//Print the sorted array based on its frequency
printf ("\n Sorted Array based on its frequency:\n");
for (int i = 0; i < n; i++)
{
while (brr[i][1] != 0)
{
printf (" %d ", brr[i][0]);
brr[i][1]--;
}
}
}
int main ()
{
int arr[MAX][2], brr[MAX][2];
int n;
//Initialize array size
printf ("\nEnter the number of elements:\n");
scanf ("%d", &n);
//Initialize array elements
printf ("\nEnter the array elements :\n");
for (int i = 0; i < n; i++)
{
scanf ("%d", &arr[i][0]);
arr[i][1] = 0;
}
sortArray (arr, n, brr);
return 0;
}
```

Enter the number of elements: 10

Enter the array elements : 8 7 6 8 9 4 5 6 8 8

Array Elements and its frequency:

Elements Frequency

8 4

7 1

6 2

9 1

4 1

5 1

Sorted Array Elements based on their frequency:

Elements Frequency

8 4

6 2

7 1

9 1

4 1

5 1

Sorted Array based on its frequency:

8 8 8 8 6 6 7 9 4 5