C Program to Find Duplicate Elements in Array
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
How to Think About It
Algorithm
Code
#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.
Start with first element (1)
Compare 1 with elements after it: 2, 3, 2, 4, 5, 1. Found duplicate 1 at last position.
Check if 1 was already printed
No previous 1 before index 0, so print 1.
Move to second element (2)
Compare 2 with elements after it: 3, 2, 4, 5, 1. Found duplicate 2 at index 3.
Check if 2 was already printed
No previous 2 before index 1, so print 2.
Check other elements
No other duplicates found for 3, 4, 5.
| i | arr[i] | j | arr[j] | Duplicate Found | Already Printed | Action |
|---|---|---|---|---|---|---|
| 0 | 1 | 6 | 1 | Yes | No | Print 1 |
| 1 | 2 | 3 | 2 | Yes | No | Print 2 |
| 2 | 3 | - | - | No | - | No action |
| 3 | 2 | - | - | Already counted | - | No action |
| 4 | 4 | - | - | No | - | No action |
| 5 | 5 | - | - | No | - | No action |
| 6 | 1 | - | - | 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
#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; }
#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; }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Nested loops | O(n^2) | O(1) | Small arrays, no extra memory |
| Sorting | O(n log n) | O(1) | Larger arrays, order change allowed |
| Frequency array | O(n) | O(k) | Small range integers, fast detection |