Solved programming problem

Find the element with highest frequency in the array using pointer in C.

Source code

#include <stdio.h>

int fun(int a,int *pv)

{

    int v=0;

    int i,j,count=0,max=0;

    for(i=0; i<a; i++)

    {

        count=0;

        for(j=0; j<a; j++)

        {

            if(*(pv+i)==*(pv+j)) ///checking if there any same number is available or not.

            {

                count++;

            }

        }

        if(max<count)

        {

            max=count;

        }

    }

    printf("Total: %d",max);

}

int main()

{

    int i,a;

    int array[100];

    int *pv;

    printf("Please Enter how many number you want to use in the array: ");

    scanf("%d",&a);

    for(i=0; i<a; i++)

    {

        scanf("%d",&array[i]);

    }

    pv=array; ///saving the address of array[0] in pointer

    fun(a,pv);

    return 0;

}

Output

Please Enter how many number you want to use in the array: 5
1 2 1 4 5
Total: 2

Join the discussion

Add your comment