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 remove any duplicate values, leaving only the unique elements. This is a common problem in programming, as handling duplicate values efficiently is important in many real-world applications.

This exercise will help you practice working with arrays, utilizing loops and conditional statements to identify duplicates, and creating an efficient way to filter them out. It's a useful skill for data processing tasks such as cleaning data before analysis.

Group

Arrays and Vectors in C++

Objective

1. Define an array with some duplicate numbers.
2. Implement a function that iterates through the array and removes any duplicates.
3. Store the result in a new array or update the original array to only contain unique elements.
4. Output the new array after removing duplicates.
5. Test the program with different arrays to ensure it works correctly.

Create a program that removes duplicates from an array of numbers.

Example C++ Exercise

 Copy C++ Code
#include <iostream> // Include iostream for input and output
#include <unordered_set> // Include unordered_set to easily track unique elements
using namespace std; // Use the standard namespace

// Function to remove duplicates from an array
void removeDuplicates(int arr[], int& size) {
    unordered_set<int> uniqueElements; // Declare a set to store unique elements
    int index = 0; // Variable to track position in the original array

    // Iterate through the array
    for (int i = 0; i < size; i++) {
        // If the element is not already in the set, add it
        if (uniqueElements.find(arr[i]) == uniqueElements.end()) {
            arr[index++] = arr[i]; // Assign the unique element to the current index
            uniqueElements.insert(arr[i]); // Insert the element into the set
        }
    }

    size = index; // Update the size of the array after removing duplicates
}

int main() {
    // Define an array with duplicates
    int arr[] = {1, 2, 2, 3, 4, 4, 5, 5, 6};
    int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array

    // Call the function to remove duplicates
    removeDuplicates(arr, size);

    // Output the array after removing duplicates
    cout << "Array after removing duplicates: ";
    for (int i = 0; i < size; i++) {
        cout << arr[i] << " "; // Print each element of the unique array
    }
    cout << endl;

    return 0; // End of program
}

 Output

Array after removing duplicates: 1 2 3 4 5 6

Share this C++ Exercise


More C++ Programming Exercises of Arrays and Vectors in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Check if an Array is Symmetric in C++

    In this exercise, you will create a function to determine if an array is symmetric. An array is considered symmetric if it reads the same forward and backward, meaning the first el...

  • Sort an Array of Numbers in Ascending Order in C++

    In this exercise, you will implement a program that sorts an array of numbers in ascending order. Sorting is one of the most fundamental operations in programming, and there are se...

  • Sum of Array Elements in C++

    In this exercise, you will create a program that calculates the sum of all the elements in an array. Summing the elements of an array is a common task in programming, which is ofte...

  • Search for a Number in an Array in C++

    In this exercise, you will create a program that searches for a specific number in an array. Searching through an array is a common task in programming, which is used in various ap...

  • Calculate the Average of Array Elements in C++

    In this exercise, you will create a function that calculates the average of the elements in an array. This is a common task when working with arrays, especially in data analysis, w...

  • Remove a Specific Element from an Array in C++

    In this exercise, you will develop a program that removes a specific element from an array. Removing elements from an array is a common task, especially when working with data that...