C++ Program to Find duplicates in an Array

 

 

Duplicate Elements: Find duplicates in an array

This program demonstrates how to find duplicate elements in an array using C++. The algorithm iterates through the array and uses a set to track seen elements. If an element is found again, it is identified as a duplicate.

C++ Program

#include <iostream>
#include <unordered_set>
#include <vector>

void findDuplicates(const std::vector<int>& arr) {
    std::unordered_set<int> seen;
    std::unordered_set<int> duplicates;

    for (int element : arr) {
        if (seen.find(element) != seen.end()) {
            duplicates.insert(element);
        } else {
            seen.insert(element);
        }
    }

    std::cout << "Duplicate elements: ";
    if (duplicates.empty()) {
        std::cout << "No duplicates found.";
    } else {
        for (int element : duplicates) {
            std::cout << element << " ";
        }
    }
    std::cout << std::endl;
}

int main() {
    std::vector<int> array = {1, 2, 3, 4, 5, 6, 7, 2, 3, 8, 9, 10, 10};
    findDuplicates(array);
    return 0;
}

Explanation

The program includes the following components:

  • #include <iostream>: Includes the standard input/output stream library.
  • #include <unordered_set>: Includes the unordered set library for using sets to track seen elements.
  • #include <vector>: Includes the vector library for using dynamic arrays.
  • void findDuplicates(const std::vector<int>& arr): Defines a function that takes a vector of integers as input and prints the duplicate elements.
  • std::unordered_set<int> seen: Initializes a set to keep track of elements that have been encountered in the array.
  • std::unordered_set<int> duplicates: Initializes a set to store duplicate elements.
  • for (int element : arr): Iterates through each element in the array.
  • if (seen.find(element) != seen.end()): Checks if the element is already in the seen set. If it is, the element is a duplicate.
  • duplicates.insert(element): Adds the duplicate element to the duplicates set.
  • seen.insert(element): Marks the element as seen.
  • std::cout << "Duplicate elements: ": Prints the start of the duplicate elements output.
  • if (duplicates.empty()): Checks if no duplicates were found and prints an appropriate message.
  • int main(): The main function that initializes a vector and calls the findDuplicates function.
  • std::cout << element << " ": Prints each duplicate element.

The example vector std::vector<int> array = {1, 2, 3, 4, 5, 6, 7, 2, 3, 8, 9, 10, 10}; contains the duplicates 2, 3, and 10. When the findDuplicates function is called, it will print:

Duplicate elements: 2 3 10

 

Explanation

  • C++ Program: The provided C++ program utilizes unordered_set from the Standard Library to identify duplicate elements in an array. The findDuplicates function iterates through the input array, using a set called seen to track elements that have been encountered. If an element is already in the seen set, it is added to the duplicates set.
  • Output: The output of the program will list all the duplicate elements found in the array.

3 Replies to “C++ Program to Find duplicates in an Array”

Leave a Reply

Your email address will not be published. Required fields are marked *