Count distinct elements in an array

Problem statement:- Program to Count distinct elements in an array.

Data requirement:-

   Input Data:- arr,size

  Output Data:-count

  Additional Data:- i, j, max, freq

Program in C

Here is the source code of the C Program to Count distinct elements in an array.

Code:

#include<stdio.h>
#include<limits.h>
main()
{
    printf("Enter the size of the array:");
    int size,j;
    scanf("%d",&size);
    int arr[size],max=INT_MIN,count=0;
    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]]++;
    for(j=0;j<max+1;j++)
        {
            if(freq[j]==1)
                count++;
        }
        printf("Numbers of distinct elements are %d",count);

}

Input/Output:
Enter the size of the array:5
Enter the Element of the array:
4
5
4
3
9
Numbers of distinct elements are 3

Program in C++

Here is the source code of the C++ Program to Count distinct elements in an array.

Code:

#include<iostream>
#include<climits>
using namespace std;
main()
{
    cout<<"Enter the size of the array:";
    int size,j;
    cin>>size;
    int arr[size],max=INT_MIN,count=0;
    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]]++;
    for(j=0;j<max+1;j++)
        {
            if(freq[j]==1)
                count++;
        }
        cout<<"Numbers of distinct elements are "<<count;
}

Input/Output:
Enter the size of the array:4
Enter the Element of the array:
9
3
7
5
Numbers of distinct elements are 4

Program in Java

Here is the source code of the Java Program to Count distinct elements in an array.

Code:

import java.util.Scanner;
public class p26 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
    int size,j,count=0;
    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]]++;
    for(j=0;j<freq.length;j++)
        {
    if(freq[j]==1)
                count++;
              }
    System.out.println("Numbers of distinct elements are "+count);

    sc.close();
    }
}

Input/Output:
Enter the size of the array:
6
Enter the Element of the array:
77
89
32
77
89
45
Numbers of distinct elements are 2

Program in Python

Here is the source code of the Python Program to Count distinct elements 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
count=0
for i in range(0, max+1):
    if freq[i] == 1:
        count+=1
print("Numbers of distinct elements are ",count)

Input/Output:
Enter the size of the array: 5
Enter the Element of the array:
25
52
37
58
52
Numbers of distinct elements are  3


Post a Comment

0 Comments