Remove Duplicates from an Array

Remove Duplicates from an Array in C++

In this exercise, you will create a program that removes duplicates from an array of numbers. The function will iterate over the array and filter out any repeated values, leaving only the unique elements.

Handling duplicate data is a vital skill for data cleaning and optimization, ensuring that algorithms process only necessary information. You will learn to use auxiliary data structures like sets to track uniqueness efficiently.

Logic Components

1. Unordered Set

We use std::unordered_set for O(1) average lookup time to instantly check if a value has been seen before.

2. In-Place Update

A separate index counter allows us to overwrite the original array with unique values, saving memory.

3. Dynamic Sizing

By passing the size by reference (int& size), the function updates the caller's array length automatically.

Learning Objectives

  • 1. Utilize unordered_set for high-performance membership testing.
  • 2. Implement index tracking to modify arrays in-place.
  • 3. Understand Pass-by-Reference to update variable values outside functions.
  • 4. Differentiate between original data and filtered results.

Solution Implementation

remove_duplicates.cpp Hashing Logic
#include <iostream>
#include <unordered_set> 
using namespace std;

// Function to remove duplicates using a hash set
void removeDuplicates(int arr[], int& size) {
    unordered_set<int> uniqueElements;
    int index = 0; // Tracks the next position for a unique element

    for (int i = 0; i < size; i++) {
        // If element is not found in the set, it is unique
        if (uniqueElements.find(arr[i]) == uniqueElements.end()) {
            arr[index++] = arr[i]; 
            uniqueElements.insert(arr[i]);
        }
    }

    size = index; // Update the size reference
}

int main() {
    int arr[] = {1, 2, 2, 3, 4, 4, 5, 5, 6};
    int size = sizeof(arr) / sizeof(arr[0]);

    removeDuplicates(arr, size);

    cout << "Array after removing duplicates: ";
    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;

    return 0;
}

Console Output:

Array after removing duplicates: 1 2 3 4 5 6
Related Topics
Unordered Set Data Cleaning Pass by Reference Membership Testing O(1) Average Time In-Place Filtering

Share this Training

Help others clean up their C++ arrays!

© C++ Programming Exercises. Master the art of performance coding.