Write a C++ Program to count distinct value in an array| AskTheCode

Asked by: Umar Khan | Category: C++ ( c plus plus ) | Program to count distinct elements in an array in C++

#include <iostream>

using namespace std;

int distinctValueCounter(int a[], int size){

int i, j, count = 1;

for (i = 1; i < size; i++){

for (j = 0; j < i; j++){

if (a[i] == a[j]){




if (i == j){




return count;


int main(){

int n;

cout<< "Enter the number of elements: ";


int arr[n];

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

cout<< "Enter "<<i+1 <<"th element: ";



cout << "\nThe array is:";

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

cout << " "<<arr[i];


cout << "\nThe number of distinct elements is: "<<distinctValueCounter(arr, n);

return 0;


24 views0 comments

Recent Posts

See All

C Program To Add Two User Given Polynomials

C program to add two polynomials | Ask The Code C Program to find the result of addition of two given polynomials by the user...Read more...

"Believe you can and you're halfway there"