Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
C Program to delete the duplicate elements in an array
In C programming, removing duplicate elements from an array means creating a new array that contains only unique elements. This is a common array manipulation task that helps eliminate redundancy in data.
Syntax
for(i = 0; i < size; i++) {
for(j = i + 1; j < size; j++) {
if(arr[i] == arr[j]) {
// Shift elements left to remove duplicate
for(k = j; k < size - 1; k++) {
arr[k] = arr[k + 1];
}
size--; // Reduce array size
j--; // Check current position again
}
}
}
Method 1: In-Place Removal Using Nested Loops
This approach removes duplicates by shifting elements left whenever a duplicate is found −
#include <stdio.h>
int main() {
int a[50], i, j, k, number;
printf("Enter size of the array: ");
scanf("%d", &number);
printf("Enter elements of the array: ");
for(i = 0; i < number; i++) {
scanf("%d", &a[i]);
}
printf("Original array: ");
for(i = 0; i < number; i++) {
printf("%d ", a[i]);
}
/* Remove duplicates using nested loops */
for(i = 0; i < number; i++) {
for(j = i + 1; j < number; j++) {
if(a[i] == a[j]) {
/* Shift elements left to remove duplicate */
for(k = j; k < number - 1; k++) {
a[k] = a[k + 1];
}
number--; /* Reduce array size */
j--; /* Check current position again */
}
}
}
printf("\nAfter removing duplicates: ");
for(i = 0; i < number; i++) {
printf("%d ", a[i]);
}
printf("<br>");
return 0;
}
Enter size of the array: 8 Enter elements of the array: 1 2 3 2 4 3 5 1 Original array: 1 2 3 2 4 3 5 1 After removing duplicates: 1 2 3 4 5
Method 2: Using Separate Array for Unique Elements
This approach creates a new array to store only unique elements −
#include <stdio.h>
int main() {
int arr[50], unique[50], i, j, n, uniqueCount = 0;
int isUnique;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements: ");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Original array: ");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
/* Find unique elements */
for(i = 0; i < n; i++) {
isUnique = 1;
/* Check if element already exists in unique array */
for(j = 0; j < uniqueCount; j++) {
if(arr[i] == unique[j]) {
isUnique = 0;
break;
}
}
/* Add to unique array if not found */
if(isUnique) {
unique[uniqueCount] = arr[i];
uniqueCount++;
}
}
printf("\nUnique elements: ");
for(i = 0; i < uniqueCount; i++) {
printf("%d ", unique[i]);
}
printf("<br>");
return 0;
}
Enter number of elements: 6 Enter elements: 5 3 7 3 5 9 Original array: 5 3 7 3 5 9 Unique elements: 5 3 7 9
Key Points
- Time Complexity: Both methods have O(n²) time complexity due to nested loops.
- Space Complexity: Method 1 uses O(1) extra space; Method 2 uses O(n) extra space.
- Method 1 modifies the original array, while Method 2 preserves it.
- Both methods maintain the order of first occurrence of elements.
Conclusion
Removing duplicates from an array can be achieved using in-place shifting or creating a separate array for unique elements. The choice depends on whether you need to preserve the original array and space constraints.
Advertisements
