Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to remove duplicates in-place from an unsorted integer array in C++

1 Answer

0 votes
#include <iostream>
#include <unordered_set>

int removeDuplicates(int arr[], int size) {
    std::unordered_set<int> seen;
    int index = 0;
    
    for (int i = 0; i < size; i++) {
        if (seen.find(arr[i]) == seen.end()) {
            seen.insert(arr[i]);
            arr[index++] = arr[i];  // overwrite in-place
        }
    }
    
    return index; // new size of array
}

int main() {
    int arr[] = {4, 3, 2, 5, 4, 5, 1, 4, 5, 2, 3, 2};
    int size = sizeof(arr) / sizeof(arr[0]);
    
    size = removeDuplicates(arr, size);
    
    for (int i = 0; i < size; i++) std::cout << arr[i] << " ";
}



/*
run:

4 3 2 5 1 

*/

 



answered Nov 30, 2025 by avibootz

Related questions

2 answers 200 views
3 answers 252 views
1 answer 58 views
2 answers 123 views
3 answers 207 views
207 views asked Apr 12, 2020 by avibootz
...