Program to Delete Duplicate Element From an Array

Program to Delete Duplicate Element From an Array

  • Write a program to Delete Duplicate Element From an Array in C
  • Write a program to Delete Duplicate Element From an Array in C++
  • Write a program to Delete Duplicate Element From an Array in Python
  • Write a program to Delete Duplicate Element From an Array in PHP
  • Write a program to Delete Duplicate Element From an Array in Java
  • Write a program to Delete Duplicate Element From an Array in Java Script
  • Write a program to Delete Duplicate Element From an Array in C#

Explanation:

The objective is to keep only unique elements in an array after deleting duplicates. The reasoning and implementation are shown below:

Logic:

  1. Initialize an Empty Data Structure:
    • Use a data structure like a set or hash map to keep track of seen elements (to identify duplicates).
  2. Iterate Through the Array:
    • Check if the current element exists in the “seen” data structure:
      • If not, add it to the “seen” structure and include it in the result.
      • If it does exist, skip it.
  3. Output:
    • Return or overwrite the array with the unique elements.

Program to Delete Duplicate Element From an Array

#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 2, 4, 5, 5};  // Example array
    int size = sizeof(arr) / sizeof(arr[0]);
    int i, j, k;

    // Remove duplicates
    for (i = 0; i < size; i++) {
        for (j = i + 1; j < size; j++) {
            if (arr[i] == arr[j]) {
                for (k = j; k < size - 1; k++) {
                    arr[k] = arr[k + 1];
                }
                size--;  // Decrease the size of the array
                j--;     // Stay at the same index to check the new element
            }
        }
    }

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

    return 0;
}

#include <iostream>
#include <set>
using namespace std;

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

    set<int> uniqueElements;  // Set automatically handles duplicates

    for (int i = 0; i < size; i++) {
        uniqueElements.insert(arr[i]);  // Insert elements into set
    }

    cout << "Array after removing duplicates: ";
    for (auto elem : uniqueElements) {
        cout << elem << " ";
    }
    cout << endl;

    return 0;
}

arr = [1, 2, 3, 2, 4, 5, 5]  # Example array

arr = list(set(arr))  # Convert list to set to remove duplicates, then back to list

print("Array after removing duplicates:", arr)

<?php
$arr = array(1, 2, 3, 2, 4, 5, 5);  // Example array

$arr = array_values(array_unique($arr));  // Remove duplicates

echo "Array after removing duplicates: ";
print_r($arr);
?>

import java.util.HashSet;

public class Main {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 2, 4, 5, 5};  // Example array
        HashSet<Integer> uniqueElements = new HashSet<>();

        for (int num : arr) {
            uniqueElements.add(num);  // HashSet automatically removes duplicates
        }

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

const arr = [1, 2, 3, 2, 4, 5, 5];  // Example array

const uniqueArr = [...new Set(arr)];  // Use Set to remove duplicates

console.log("Array after removing duplicates:", uniqueArr);

using System;
using System.Linq;

class Program {
    static void Main() {
        int[] arr = {1, 2, 3, 2, 4, 5, 5};  // Example array

        var uniqueArr = arr.Distinct().ToArray();  // Remove duplicates using LINQ

        Console.Write("Array after removing duplicates: ");
        foreach (var num in uniqueArr) {
            Console.Write(num + " ");
        }
        Console.WriteLine();
    }
}

List of All Programs