Sort elements by frequency of occurrences code in C

Sort elements by frequency of occurrences

Here, in this page we will discuss the C program to sort array elements in such a way that the element with the highest number of occurrences comes first. If the number of occurrences are equal then the print the number which appeared first in the array.

Sort elements by frequency of occurrences code in C 1

Sort Elements by Frequency of Occurrences in C

To solve sort elements by frequency of occurrences in C we can use an array to count the occurrences of each element , and then use a sorting algorithm to sort the elements by their counts . Lets see this in detail .

Sort elements by frequency of occurrences code in C

Algorithm

  • Take the size of the array and store it in variable n.
  • Declare two array of n size and named them as arr[] and mp[].
  • Take the elements of the array from the user and store them in one array.
  • Calculate the frequency of each element of the array and store it in frequency array.
  • Then sort the frequency array. And print the elements according to their frequency count.

Overall, this algorithm has a time complexity of O(n^2), where n is the size of the input array, due to the use of an insertion sort algorithm. If a more efficient sorting algorithm is used, such as quicksort or mergesort, the time complexity can be improved to O(n log n). However, the basic idea of using an array to count the occurrences of each element and then sorting by the counts remains the same.

Code for Sort Elements by Frequency of Occurrences in C​

Run
#include <stdio.h>

int main ()
{
  int k = 0, n, temp, count;

  printf ("Enter the number of elements:\n");
  scanf ("%d", &n);

  int arr[n][2], mp[n][2];

  printf ("\nEnter the array elements :\n");
  for (int i = 0; i < n; i++)
    {
      scanf ("%d", &arr[i][0]);
      arr[i][1] = 0;
    }

  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++;
	    }
	}

      mp[k][0] = arr[i][0];
      mp[k][1] = count;
      k++;


    }
  n = k;

  for (int i = 0; i < n - 1; i++)
    {

      temp = mp[i][1];
      for (int j = i + 1; j < n; j++)
	{

	  if (temp < mp[j][1])
	    {
	      temp = mp[j][1];
	      mp[j][1] = mp[i][1];
	      mp[i][1] = temp;

	      temp = mp[j][0];
	      mp[j][0] = mp[i][0];
	      mp[i][0] = temp;
	    }
	}

    }


  printf ("\n Sorted Array based on its frequency:\n");
  for (int i = 0; i < n; i++)
    {

      while (mp[i][1] != 0)
	{

	  printf (" %d  ", mp[i][0]);
	  mp[i][1]--;

	}

    }
  return 0;
} 

Output

Enter the number of elements:
10

Enter the array elements :
1 2 3 3 2 4 1 4 4 5 

Sorted Array based on its frequency:
 4   4   4   2   2   3   3   1   1   5 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java