# Count distinct elements in an array in C++

C++Server Side ProgrammingProgramming

We are given an unsorted array of any size containing repetitive elements and the task is to calculate the count of distinct elements in an array.

Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

## For example

Input− int arr[] = {1, 1, 2, 3, 3, 4, 4}
Output − count is 4

Explanation − In the given array there are 4 distinct elements and those are 1, 2, 3, 4 but the size of array is 7 as it contains repetitive elements and our task was to remove the duplicates and then count the array elements.

Input − int arr[] = {1, 2, 3, 4, 5, 5, 5, 5}
Output − count is 5

Explanation − In the given array there are 5 distinct elements and those are 1, 2, 3, 4 and 5 but the size of array is 8 as it contains repetitive elements and our task was to remove the duplicates and then count the array elements.

## Approach used in the below program is as follows

### Using sort function()

• Create an array of let’s say, arr[]

• Calculate the length of an array using the length() function that will return an integer value as per the elements in an array.

• Call the sort function and pass the array and the size of an array as a parameter.

• Take a temporary variable that will store the count of distinct elements.

• Start a loop for with i to 0 till i is less than the size of an array

• Inside the loop, run while i < size-1 and arr[i] = arr[i+1]

• Inside the while, increment the value of i

• And inside for, increment the value of count

• Return count

• Print the result.

### Without sorting

• Create an array of let’s say, arr[]

• Calculate the length of an array using the length() function that will return an integer value as per the elements in an array.

• Take a temporary variable that will store the count of distinct elements.

• Start a loop for with i to 1 till i is less than the size of an array

• Inside the loop, set j to 0 and start another loop for with j to 0 and j less than i and increment j wth 1

• Inside this loop, check if arr[i] = arr[j] then break

• Inside this loop, check if i = j then increment the count by 1

• Return count

• Print the result.r

## Example

### With Sorting

Live Demo

#include <algorithm>
#include <iostream>
using namespace std;
int distinct_elements(int arr[], int n){
// Sorting the array
sort(arr, arr + n);
// Traverse the sorted array
int count = 0;
for (int i = 0; i < n; i++){
// Moving the index when duplicate is found
while (i < n - 1 && arr[i] == arr[i + 1]){
i++;
}
count++;
}
return count;
}
// Main Function
int main(){
int arr[] = { 3, 6, 5, 8, 2, 3, 4 };
int n = sizeof(arr) / sizeof(arr);
cout <<"count is "<<distinct_elements(arr, n);
return 0;
}

## Output

If we run the above code we will get the following output −

count is 6

## Example

### Without Sorting

Live Demo

#include <iostream>
using namespace std;
int countDistinct(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]){
break;
}
}
if (i == j){
count++;
}
}
return count;
}
// Main function
int main(){
int a[] = { 3, 6, 5, 8, 2, 3, 4 };
int size = sizeof(a) / sizeof(a);
cout << "count is "<<countDistinct(a, size);
return 0;
}

## Output

If we run the above code we will get the following output −

count is 6