0
0
CProgramBeginner · 2 min read

C Program to Find Duplicate Elements in Array

To find duplicate elements in an array in C, use nested loops to compare each element with others and print elements that appear more than once, like for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (arr[i] == arr[j]) { printf("%d ", arr[i]); } } }.
📋

Examples

Inputarr = {1, 2, 3, 2, 4, 5, 1}, n = 7
OutputDuplicate elements are: 1 2
Inputarr = {10, 20, 30, 40, 50}, n = 5
OutputNo duplicate elements found.
Inputarr = {5, 5, 5, 5}, n = 4
OutputDuplicate elements are: 5
🧠

How to Think About It

To find duplicates, check each element against all others in the array. When you find two elements that are the same, note that element as a duplicate. Avoid printing the same duplicate multiple times by keeping track of already found duplicates.
📐

Algorithm

1
Get the size of the array and the array elements.
2
Create a loop to pick each element one by one.
3
For each element, create another loop to compare it with the rest of the elements.
4
If a match is found and it is not already recorded as a duplicate, print it.
5
If no duplicates are found after checking all elements, print a message saying no duplicates.
💻

Code

c
#include <stdio.h>

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

    printf("Duplicate elements are: ");
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (arr[i] == arr[j]) {
                int alreadyPrinted = 0;
                for (int k = 0; k < i; k++) {
                    if (arr[k] == arr[i]) {
                        alreadyPrinted = 1;
                        break;
                    }
                }
                if (!alreadyPrinted) {
                    printf("%d ", arr[i]);
                    foundDuplicate = 1;
                }
                break;
            }
        }
    }
    if (!foundDuplicate) {
        printf("No duplicate elements found.");
    }
    printf("\n");
    return 0;
}
🔍

Dry Run

Let's trace the array {1, 2, 3, 2, 4, 5, 1} through the code to find duplicates.

1

Start with first element (1)

Compare 1 with elements after it: 2, 3, 2, 4, 5, 1. Found duplicate 1 at last position.

2

Check if 1 was already printed

No previous 1 before index 0, so print 1.

3

Move to second element (2)

Compare 2 with elements after it: 3, 2, 4, 5, 1. Found duplicate 2 at index 3.

4

Check if 2 was already printed

No previous 2 before index 1, so print 2.

5

Check other elements

No other duplicates found for 3, 4, 5.

iarr[i]jarr[j]Duplicate FoundAlready PrintedAction
0161YesNoPrint 1
1232YesNoPrint 2
23--No-No action
32--Already counted-No action
44--No-No action
55--No-No action
61--Already counted-No action
💡

Why This Works

Step 1: Compare each element with others

The nested loops let us check every pair of elements to find if any two are the same.

Step 2: Avoid printing duplicates multiple times

Before printing a duplicate, we check if it was already printed by looking at earlier elements.

Step 3: Print duplicates or no duplicates message

If duplicates are found, print them; otherwise, print a message saying no duplicates exist.

🔄

Alternative Approaches

Using sorting
c
#include <stdio.h>
#include <stdlib.h>

int compare(const void *a, const void *b) {
    return (*(int*)a - *(int*)b);
}

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

    qsort(arr, n, sizeof(int), compare);

    printf("Duplicate elements are: ");
    for (int i = 0; i < n - 1; i++) {
        if (arr[i] == arr[i + 1]) {
            if (i == 0 || arr[i] != arr[i - 1]) {
                printf("%d ", arr[i]);
                foundDuplicate = 1;
            }
        }
    }
    if (!foundDuplicate) {
        printf("No duplicate elements found.");
    }
    printf("\n");
    return 0;
}
Sorting makes duplicates appear next to each other, simplifying detection but changes original array order.
Using a frequency array (only for small positive integers)
c
#include <stdio.h>

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

    for (int i = 0; i < n; i++) {
        freq[arr[i]]++;
    }

    printf("Duplicate elements are: ");
    for (int i = 1; i <= max; i++) {
        if (freq[i] > 1) {
            printf("%d ", i);
            foundDuplicate = 1;
        }
    }
    if (!foundDuplicate) {
        printf("No duplicate elements found.");
    }
    printf("\n");
    return 0;
}
This method is fast but only works if array elements are small positive integers and known max.

Complexity: O(n^2) time, O(1) space

Time Complexity

The nested loops each run up to n times, so the time grows roughly with the square of the array size.

Space Complexity

No extra arrays or data structures are used, so space is constant regardless of input size.

Which Approach is Fastest?

Sorting-based approach runs in O(n log n) time and is faster for large arrays but changes the array order. Frequency array is fastest O(n) but limited to small integer ranges.

ApproachTimeSpaceBest For
Nested loopsO(n^2)O(1)Small arrays, no extra memory
SortingO(n log n)O(1)Larger arrays, order change allowed
Frequency arrayO(n)O(k)Small range integers, fast detection
💡
Use a nested loop and track printed duplicates to avoid repeating the same duplicate output.
⚠️
Beginners often print duplicates multiple times without checking if they were already printed.