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 theseen
set. If it is, the element is a duplicate.duplicates.insert(element)
: Adds the duplicate element to theduplicates
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 thefindDuplicates
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. ThefindDuplicates
function iterates through the input array, using a set calledseen
to track elements that have been encountered. If an element is already in theseen
set, it is added to theduplicates
set. - Output: The output of the program will list all the duplicate elements found in the array.
Howdy! This post couldn’t be written any better!
Reading through this post reminds me of my good old room mate!
He always kept talking about this. I will forward this article to
him. Pretty sure he willl have a good read. Many thanks foor sharing! https://Odessaforum.biz.ua/
Howdy! This post couldn’t be written any better!
Reading through this posdt reminds me of my good old room mate!
He always kept talking about this. I will forward thus article to him.
Pretty sure he will have a gokod read. Many thanks for
sharing! https://Odessaforum.biz.ua/