C++ Program to Count the Dupicate Elements in an Array Last Updated : 04 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to count the duplicate elements in an array in C++. Examples: Input: myArray = {1, 2, 2, 3, 3, 3}; Output: Number of Duplicates: 3Find the Number of Duplicate Elements in an Array in C++To count the duplicate elements in an array in C++, we can use a std::set container which only stores the unique elements. We can store the array elements in the set while traversing the array and if the element is already present, then it means that the current element is duplicate. ApproachInside the function, initialize a set to store the unique elements from the array, and an integer to keep track of the count of duplicate elements. Traverse the array using a loop. For each element in the array, do the following:Use the std::set::find() function of the set to check if the current element is already in the set.If the element is in the set, it means it’s a duplicate. So, increment the count of duplicate elements.If the element is not in the set, it means it’s the first time we’re seeing this element. So, insert it into the set.After the loop, return the count of duplicate elements.C++ Program to Count the Duplicate Elements in an Array C++ // C++ Program to illustrate how to Find the Number of // Duplicate Elements in an Array #include <iostream> #include <set> #include <vector> using namespace std; int countDuplicates(int* arr, int n) { // Set to store unique elements set<int> uniqueSet; // Counter for duplicate elements int duplicateCount = 0; for (int i = 0; i < n; i++) { if (uniqueSet.find(arr[i]) != uniqueSet.end()) { // If element is already present in the set, // increment duplicate count duplicateCount++; } else { // If element is not present in the set, add it // to the set uniqueSet.insert(arr[i]); } } return duplicateCount; } // Driver code int main() { // Input array int arr[] = { 12, 11, 40, 12, 5, 6, 5, 12, 11 }; int n = sizeof(arr) / sizeof(arr[0]); int duplicates = countDuplicates(arr, n); cout << "Number of Duplicate elements are " << duplicates; return 0; } OutputNumber of Duplicate elements are 4Time Complexity: O(N log N), where N is the size of the array.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article C++ Program to Count the Dupicate Elements in an Array N nikitamehrotra99 Follow Improve Article Tags : C++ Programs C++ STL cpp-array cpp-set CPP Examples +2 More Practice Tags : CPPSTL Similar Reads C++ Program for Last duplicate element in a sorted array We have a sorted array with duplicate elements and we have to find the index of last duplicate element and print index of it and also print the duplicate element. If no such element found print a message. Examples: Input : arr[] = {1, 5, 5, 6, 6, 7} Output : Last index: 4 Last duplicate item: 6 Inpu 2 min read C++ Program to Find the Frequency of Elements in an Array In C++, arrays are a type of data structure that can store a fixed-size sequential collection of elements of the same type. In this article, we will learn how to find the frequency of elements in an array in C++. Example: Input: Array: {1, 2, 3, 4, 2, 1, 3, 2, 4, 5} Output: Element: 1, Frequency: 2 2 min read C++ Program To Remove Duplicates From Sorted Array Given a sorted array, the task is to remove the duplicate elements from the array.Examples: Input: arr[] = {2, 2, 2, 2, 2} Output: arr[] = {2} new size = 1 Input: arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5} Output: arr[] = {1, 2, 3, 4, 5} new size = 5 Recommended PracticeRemove duplicate elements from sorte 3 min read How to Find the Sum of Elements in an Array in C++? In C++, an array is the collection of similar data elements that are stored in the contiguous memory location and we can access these elements directly by their index value. In this article, we will learn how to find the sum of elements in an array in C++. Example:Input: myVector = {1, 2, 3, 4, 5} O 2 min read How to Find the Unique Elements in an Array in C++? In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the unique elements in an array in C++. Example: Input: array = {1, 2, 1, 2, 2, 3, 4} Output: Unique elements in the array: 1 2 2 min read How to Find the Length of an Array in C++? In C++, the length of an array is defined as the total number of elements present in the array. In this article, we will learn how to find the length of an array in C++.The simplest way to find the length of an array is by using the sizeof operator. First calculate the size of the array in bytes and 2 min read Count the number of clumps in the given Array Given an array arr[] of N integers, the task is to count the number of clumps in the given array. Clump is defined as a series of 2 or more adjacent elements of the same value. Examples: Input: arr[] = { 13, 15, 66, 66, 37, 8, 8, 11, 52 }; Output: 2 Explanation: There are two clumps in the given arr 5 min read How to Calculate the Size of a Static Array in C++? In C++, static arrays are the type of arrays whose size is fixed, and memory for them is allocated during the compile time. In this article, we will learn how to calculate the size of a static array in C++. Example: Input:myArray = {1, 2, 3, 4, 5}Output:The size of the array is 5.Size of a Static Ar 2 min read How to Count the Number of Occurrences of a Value in an Array in C++? In C++, an array is a data structure that stores the collection of the same type of data in a contiguous memory location. In this article, we will learn how to count the number of occurrences of a value in an array in C++. Example: Input: arr= {2, 4, 5 ,2 ,4 , 5, 2 , 3 ,8}Target = 2Output: Number of 2 min read How to Print an Array in C++? In C++, an array is a fixed-size linear data structure that stores a collection of elements of the same type in contiguous memory locations. In this article, we will learn how to print an array in C++. For Example, Input: array = {10, 20, 30, 40, 50}Output: Array Elements: 10 20 30 40 50Printing Arr 2 min read Like