Removing Duplicates Elements from an Array

Definition & Explanation

This program is designed to remove duplicate elements from a given array of integers while maintaining the original order of elements. Duplicate elements are those that appear more than once in the array. The program implements a simple algorithm that iterates through the array, keeping track of elements encountered so far using a set or hash table data structure. As each element is encountered, it is checked against the set of unique elements.

If the element is not already in the set, it is added to the set and appended to a new array that stores unique elements. If the element is already present in the set, it is skipped, effectively removing duplicates. Finally, the program outputs the new array containing only unique elements. This functionality is encapsulated within a function or method, making it reusable for different arrays. The program demonstrates the use of set or hash table data structures to efficiently remove duplicate elements from an array while preserving the original order of elements.

Program Logic

  1. Initialize Data Structures:
    • Create an empty set or hash table to store unique elements encountered so far.
    • Create an empty array to store the unique elements from the original array.
  2. Iterate Through the Array:
    • Start iterating through the input array.
  3. Check for Duplicate Elements:
    • For each element in the array:
      • Check if the element is already present in the set or hash table.
        • If not present, add the element to the set and append it to the new array.
        • If already present, skip the element (as it is a duplicate).
  4. Repeat:
    • Repeat step 3 for all elements in the array.
  5. Output:
    • Once the iteration is complete, the new array will contain only unique elements in the same order as the original array.
    • Output this new array as the result.

This logic ensures that the program efficiently removes duplicate elements from the given array while preserving the original order of elements. It uses a set or hash table data structure to keep track of unique elements encountered so far, allowing for quick lookups to identify duplicates.

C

Method 1 :

#include <stdio.h>

void removeDuplicates(int arr[], int n) {
    if (n <= 1) {
        return; // No duplicates possible if array has 0 or 1 element
    }

    int set[n];
    int uniqueCount = 0;

    for (int i = 0; i < n; i++) {
        int j;
        for (j = 0; j < uniqueCount; j++) {
            if (arr[i] == set[j]) {
                break; // Element is duplicate, skip
            }
        }
        if (j == uniqueCount) {
            set[uniqueCount++] = arr[i]; // Add unique element to set
        }
    }

    // Copy unique elements back to original array
    for (int i = 0; i < uniqueCount; i++) {
        arr[i] = set[i];
    }
}

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

    removeDuplicates(arr, n);

    printf("Array after removing duplicates: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}

Output :

C++

Method 1 :

#include <iostream>
#include <unordered_set>
#include <vector>

void removeDuplicates(std::vector<int>& arr) {
    std::unordered_set<int> seen;
    std::vector<int> uniqueElements;

    for (int num : arr) {
        if (seen.find(num) == seen.end()) {
            seen.insert(num);
            uniqueElements.push_back(num);
        }
    }

    arr = uniqueElements; // Update original array with unique elements
}

int main() {
    std::vector<int> arr = {1, 2, 2, 3, 4, 4, 5};

    removeDuplicates(arr);

    std::cout << "Array after removing duplicates: ";
    for (int num : arr) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

Output :

JAVA

Method 1 :

import java.util.ArrayList;
import java.util.HashSet;

public class RemoveDuplicates {
    public static void removeDuplicates(int[] arr) {
        HashSet<Integer> set = new HashSet<>();
        ArrayList<Integer> uniqueElements = new ArrayList<>();

        for (int num : arr) {
            if (!set.contains(num)) {
                set.add(num);
                uniqueElements.add(num);
            }
        }

        // Convert ArrayList to array
        for (int i = 0; i < uniqueElements.size(); i++) {
            arr[i] = uniqueElements.get(i);
        }
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 2, 3, 4, 4, 5};

        removeDuplicates(arr);

        System.out.print("Array after removing duplicates: ");
        for (int num : arr) {
            System.out.print(num + " ");
        }
        System.out.println();
    }
}

Output :

Python

Method 1 :

def removeduplicates(arr):
    unique_ele=[]
    seen=set()
    for num in arr:
        if num not in seen:
            seen.add(num)
            unique_ele.append(num)
    return unique_ele

arr=[1,2,2,2,3,45,4,5]
result=removeduplicates(arr)
print(result)

Output :

[1, 2, 3, 45, 4, 5]