Daily C Language Challenge No. 13: Efficiently Remove Duplicate Elements from an Array

šŸ“Œ Problem Description

Write a program to input an integer array, remove duplicate elements in place, and return the new array content.

Requirements:

  • Space complexity O(1), meaning no extra array memory allocation

  • Maintain the order of elements

Example:

Input: 3 2 2 4 3 → Output: 3,2,4  Input: 5 5 5 → Output: 5  

Difficulty:ā­ļøā­ļø

šŸ’” Basic Implementation (Linear Scan Method)

#include <stdio.h>
int remove_duplicates(int arr[], int n) {    if (n == 0) return 0;
    int new_len = 1; // New array length    for (int i = 1; i < n; i++) {        int is_duplicate = 0;        // Check if the current element already exists in the new array        for (int j = 0; j < new_len; j++) {            if (arr[i] == arr[j]) {                is_duplicate = 1;                break;            }        }        if (!is_duplicate) {            arr[new_len++] = arr[i];        }    }    return new_len;}
int main() {    int n;    printf("Input array length:");    scanf("%d", &n);    int arr[n];    printf("Input %d elements:", n);    for (int i = 0; i < n; i++) scanf("%d", &arr[i]);    int x = sizeof(arr)/sizeof(arr[0]);    int new_len = remove_duplicates(arr, x);    printf("Array after removing duplicates:");    for (int i = 0; i < new_len; i++) printf("%d ", arr[i]);    return 0;}

Output Example:

Input array length: 5Input 5 elements: 3 2 2 4 3Array after removing duplicates: 3 2 4

Characteristics::

  • Time complexity O(n²): Double loop checks for duplicates

  • Space complexity O(1): In-place modification of the array

⚔ Extended Functionality

Count Duplicate Occurrences

void count_duplicates(int arr[], int n) {    int count = 0;    for (int i = 0; i < n; i++) {        if (i > 0 && arr[i] == arr[i-1]) continue;        int j = i + 1;        while (j < n && arr[j] == arr[i]) j++;        if (j - i > 1) {            printf("%d appears %d times\n", arr[i], j - i - 1);            count += j - i - 1;        }    }    printf("Total number of duplicate elements: %d\n", count);}

šŸ¤” Common Error Analysis

Issue Cause Solution
Out of bounds access Did not handle empty array correctly Add<span>if (n == 0) return 0;</span>
Consecutive duplicate elements Did not handle non-continuous duplicates (e.g., 3,2,3) Can only use the basic double loop method

šŸŽÆ Today’s Challenge

Try to implement the following functionality:

Remove duplicates while recording original positions (e.g., output the indices of elements in the original array)šŸš€ Next Preview No. 14 Count Character Types: Can You Efficiently Classify Character Types?

šŸ“¢ Interaction Time

What tricky problems have you encountered while removing duplicates? Feel free to leave a comment!

If you find this useful, please share it with friends learning programming! šŸš€

šŸš€If you find this useful, feel free to share it with friends learningC language!

Article Author:Vv Computer Graduate Examination World (focusing on computer graduate examination guidance8 years)

Original Statement: Please contact for authorization if reprinted, infringement will be pursued.

Leave a Comment