24. Find duplicates in an array
The problem can be found at the following link: Question Link
My Approach
To find duplicates in the given array,
I use an array
cnt
to keep track of the count of each element in the input arrayarr
.Then, I iterate through the
cnt
array and push the indices of elements with a count greater than 1 to theout
vector.Finally, if there are duplicates, I return the
out
vector; otherwise, I return {-1} to indicate no duplicates.
Time and Auxiliary Space Complexity
Time Complexity:
O(n)
, wheren
is the size of the input array. This is because we iterate through the input array once to count the occurrences of each element and then iterate through thecnt
array once to find duplicates.Auxiliary Space Complexity:
O(n)
, as we use an additional arraycnt
to store the counts of elements.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.
Last updated