
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Print numbers in descending order along with their frequencies
Given with an array of int elements, the task is to arrange the elements in descending order and finding their occurrences.
Input : arr[]={1,1,1,2,2,2,3,3,4,5,6,7,7} Output : 7 occurs: 2 6 occurs: 1 5 occurs: 1 4 occurs: 1 3 occurs: 2 2 occurs: 3 1 occurs: 3
Algorithm
START Step 1 -> input array with elements in sorting order Step 2 -> calculate size of an array by sizeof(a)/sizeof(a[0] Step 3 -> store size in a variable say en Step 4 -> Loop For i=siz-1 and i>0 and i== IF a[i]!=a[i-1] Set to=en-1 Print a[i] and to Set en=i End Step 5 -> print a[0] and to STOP
Example
#include<stdio.h> int main() { int a[]={1,1,1,2,2,2,3,3,4,5,6,7,7}; int siz,i,en,st,to; siz=sizeof(a)/sizeof(a[0]); en=siz; for(i=siz-1;i>0;i--) { if(a[i]!=a[i-1]) { to=en-i; printf("%d occurs: %d
",a[i],to); en=i; } } to=en; printf("%d occurs: %d
",a[0],to); }
Output
if we run the above program then it will generate the following output
7 occurs: 2 6 occurs: 1 5 occurs: 1 4 occurs: 1 3 occurs: 2 2 occurs: 3 1 occurs: 3
- Related Articles
- Print characters and their frequencies in order of occurrence in C++
- Print 2-D co-ordinate points in ascending order followed by their frequencies in C++
- Print characters having odd frequencies in order of occurrence in C++
- Sorting numbers in descending order but with `0`s at the start JavaScript
- Recursion example in JavaScript to display numbers is descending order?
- Sort ArrayList in Descending order using Comparator with Java Collections
- Sorting string in Descending order C++
- Sort MongoDB documents in descending order
- C# Program to order array elements in descending order
- How to order by timestamp (descending order) in MongoDB
- Print n smallest elements from given array in their original order
- Numbers $50, 42, 35, 2x + 10, 2x - 8, 12, 11, 8$ are written in descending order and their median is 25, find $x$.
- Print prime numbers from 1 to N in reverse order
- Name four plants along with their edible parts.
- List four plant hormones along with their functions.

Advertisements