- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Count distinct elements in an array in C++

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

#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[0]); 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

#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[0]); cout << "count is "<<countDistinct(a, size); return 0; }

## Output

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

count is 6

- Related Questions & Answers
- Count distinct elements in an array in Python
- Sum of distinct elements of an array in JavaScript
- Sum of distinct elements of an array - JavaScript
- Count subarrays having total distinct elements same as original array in C++
- Product of non-repeating (distinct) elements in an Array in C++
- Absolute distinct count in a sorted array?
- Count of subsequences having maximum distinct elements in C++
- Count number of elements in an array with MongoDB?
- Upper or lower elements count in an array in JavaScript
- Absolute distinct count in a sorted array in C++?
- Count and Sum of composite elements in an array in C++
- Print sorted distinct elements of array in C language
- Check if all array elements are distinct in Python
- Deep count of elements of an array using JavaScript
- Count distinct value in MongoDB?