Print the most occurring elements in an array

Problem statement:- Program to Print the most occurring elements in an array.

Data requirement:-

   Input Data:- arr,size

  Output Data:-most_oc, most_v

  Additional Data:- i, j, max, freq

Program in C

Here is the source code of the C Program to Print the most occurring elements in an array.

Code:

#include<stdio.h>
#include<limits.h>
int main()
{
    printf("Enter the size of the array:");
    int size;
    scanf("%d",&size);
    int arr[size],j=0,max=INT_MIN;

    printf("Enter the Element of the array:\n");
     for(j=0;j<size;j++)
    {
        scanf("%d",&arr[j]);
      if(arr[j]>=max)
      max=arr[j];
    }
        int freq[max+1];
        for(j=0;j<max+1;j++)
        {
        freq[j]=0;
        }
    for(j=0;j<size;j++)
        freq[arr[j]]++;

    int most_oc=0,most_v=0;
    for(j=0;j<max+1;j++)
        {
            if(freq[arr[j]]>most_oc)
            {
                most_oc=freq[arr[j]];
                most_v=arr[j];
            }}
                printf("\nThe Most occurring Number %d occurs %d times.",most_v,most_oc);
    }

Input/Output:
Enter the size of the array:5
Enter the Element of the array:
4
2
4
3
4

The Most occurring Number 4 occurs 3 times.

Program in C++

Here is the source code of the C++ Program to find the most frequent element in an array.

Code:

#include<iostream>
#include<climits>
using namespace std;
int main()
{
    cout<<"Enter the size of the array:";
    int size,j;
    cin>>size;
    int arr[size],max=INT_MIN;
    cout<<"Enter the Element of the array:\n";
     for(j=0;j<size;j++)
    {
    cin>>arr[j];
    if(arr[j]>=max)
    max=arr[j];
    }
        int freq[max+1];
        for(j=0;j<max+1;j++)
        {
        freq[j]=0;
        }
    for(j=0;j<size;j++)
        freq[arr[j]]++;

    int most_oc=0,most_v=0;
    for(j=0;j<size;j++)
        {
            if(freq[arr[j]]>most_oc)
            {
                most_oc=freq[arr[j]];
                most_v=arr[j];
            }}
            cout<<"\nThe Most occurring Number "<<most_v<<" occurs "<<most_oc<<" times.";
    }

Input/Output:
Enter the size of the array:5
Enter the Element of the array:
4
3
4
6
4

The Most occurring Number 4 occurs 3 times.

Program in Java

Here is the source code of the Java Program to Print the most occurring elements in an array.

Code:

import java.util.Scanner;
public class p17 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
    int size,j;
    int max=Integer.MIN_VALUE;
    size=sc.nextInt();
    int arr[ ]=new int[size];
System.out.println("Enter the Element of the array:");
     for(j=0;j<size;j++)
    {
        arr[j]=sc.nextInt();
        if(arr[j]>=max)
        max=arr[j];
    }
    int freq[]=new int[max+1]; 
    for(j=0;j<size;j++)
        freq[arr[j]]++;
    
    int most_oc=0,most_v=0;
    for(j=0;j<size;j++)
        {
            if(freq[arr[j]]>most_oc)
            {
                most_oc=freq[arr[j]];
                most_v=arr[j];
            }}
System.out.println( "The Most occurring Number "+most_v+" occurs "+most_oc+" times.");
    sc.close();   
}   
    }

Input/Output:
Enter the size of the array:
4
Enter the Element of the array:
5
4
5
5
The Most occurring Number 5 occurs 3 times.

Program in Python

Here is the source code of the Python Program to find the most frequent element in an array.

Code:

import sys
arr=[]
freq=[]
max=-sys.maxsize-1
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
    num = int(input())
    arr.append(num)
for i in range(0, size):
    if arr[i]>=max:
        max=arr[i]
for i in range(0,max+1):
    freq.append(0)
for i in range(0, size):
    freq[arr[i]]+=1
most_oc=0
most_v=0
for i in range(0, size):
    if freq[arr[i]] > most_oc:
        most_oc = freq[arr[i]]
        most_v = arr[i]
print("The Most occurring Number ",most_v," occurs ",most_oc," times.")

Input/Output:
Enter the size of the array: 5
Enter the Element of the array:
4
3
2
4
6
The Most occurring Number  4  occurs  2  times.

Post a Comment

0 Comments