- How to remove duplicate elements from an array
- How to handle unsorted arrays with duplicates
- How to handle sorted arrays with duplicates
- Step-by-step explanation of the program
- Practice exercises to test your understanding
Introduction
In this tutorial, we will learn how to write a C program to remove duplicate elements from an array.
Removing duplicates from an array is a common programming task. It is used in many real-world applications, such as:
- Cleaning up data by removing repeated entries
- Creating a unique list of items from a dataset
- Removing duplicate user records from a database
- Preparing data for analysis or reporting
š” Key Point: There are two common approaches to remove duplicates: 1) For unsorted arrays (using nested loops and a visited array) and 2) For sorted arrays (using a single pass with two pointers).
C Program to Remove Duplicates from Unsorted Array
#include <stdio.h>
int main() {
int n, i, j, k;
// Ask user for number of elements
printf("Enter the number of elements: ");
scanf("%d", &n);
// Declare array
int arr[n];
// Read elements into the array
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Remove duplicates
for(i = 0; i < n; i++) {
for(j = i + 1; j < n; j++) {
if(arr[i] == arr[j]) {
// Shift all elements to the left
for(k = j; k < n - 1; k++) {
arr[k] = arr[k + 1];
}
n--; // Decrease array size
j--; // Check the new element at position j
}
}
}
// Display array after removing duplicates
printf("\nArray after removing duplicates: ");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Sample Output
Enter the number of elements: 8 Enter 8 elements: 10 20 10 30 20 40 10 50 Array after removing duplicates: 10 20 30 40 50
Another Example:
Enter the number of elements: 6 Enter 6 elements: 5 5 5 5 5 5 Array after removing duplicates: 5
Example with No Duplicates:
Enter the number of elements: 5 Enter 5 elements: 10 20 30 40 50 Array after removing duplicates: 10 20 30 40 50
Program Explanation
Let's break down the code step by step:
- Include Header File:
#include <stdio.h>includes the standard input/output library. - Declare Variables:
int n;ā number of elementsint i, j, k;ā loop counters
- Get User Input: Reads the array elements from the user.
- Remove Duplicates:
- The outer loop (
i) iterates through each element - The inner loop (
j) checks for duplicates ofarr[i]in the remaining array - If a duplicate is found (
arr[i] == arr[j]):- Shift all elements from
j+1ton-1one position left - Decrease
nby 1 (array size reduced) - Decrease
jby 1 to check the new element at positionj
- Shift all elements from
- The outer loop (
- Display Result: Prints the array after removing duplicates.
- Return:
return 0;indicates successful program execution.
š Note: This method modifies the original array. The variable n is updated to reflect the new size after removing duplicates.
Algorithm to Remove Duplicates (Unsorted Array)
Step-by-step algorithm:
- Start
- Read the array elements
- For
i = 0ton-1:- For
j = i+1ton-1:- If
arr[i] == arr[j]:- For
k = jton-2:arr[k] = arr[k+1]
n--j--
- For
- If
- For
- Print the updated array
- End
Alternative Method: Using Visited Array
This method uses a separate array to track which elements have already been added to the result.
#include <stdio.h>
int main() {
int n, i, j;
int count = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
int visited[n]; // To mark visited elements
int unique[n]; // To store unique elements
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
visited[i] = 0;
}
// Find unique elements
for(i = 0; i < n; i++) {
if(visited[i] == 0) {
unique[count] = arr[i];
count++;
// Mark all duplicates as visited
for(j = i + 1; j < n; j++) {
if(arr[i] == arr[j]) {
visited[j] = 1;
}
}
}
}
// Display unique elements
printf("\nArray after removing duplicates: ");
for(i = 0; i < count; i++) {
printf("%d ", unique[i]);
}
printf("\n");
return 0;
}
Removing Duplicates from a Sorted Array
If the array is already sorted, we can remove duplicates in O(n) time using a two-pointer approach.
#include <stdio.h>
int main() {
int n, i, j;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d sorted elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Remove duplicates from sorted array
j = 0;
for(i = 1; i < n; i++) {
if(arr[i] != arr[j]) {
j++;
arr[j] = arr[i];
}
}
// j+1 is the new size
int newSize = j + 1;
printf("\nArray after removing duplicates: ");
for(i = 0; i < newSize; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Sample Output:
Enter the number of elements: 8 Enter 8 sorted elements: 10 10 20 20 30 40 40 50 Array after removing duplicates: 10 20 30 40 50
How Sorted Array Approach Works
- Two Pointers:
jtracks the position of the last unique element - Compare: If
arr[i] != arr[j], it's a new unique element - Copy: Copy the new element to position
j+1 - Increment: Move
jforward - Result: The first
j+1elements are unique
Time and Space Complexity
| Method | Time Complexity | Space Complexity |
|---|---|---|
| Nested Loop (Unsorted) | O(n²) | O(1) |
| Visited Array | O(n²) | O(n) |
| Sorted Array (Two-Pointer) | O(n) | O(1) |
š» Practice Exercise
Challenge 1: Modify the program to remove duplicates and sort the array in ascending order.
Challenge 2: Count the number of duplicate elements removed from the array.
š Click to Show Solution for Challenge 2
#include <stdio.h>
int main() {
int n, i, j, k;
int duplicates_removed = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int original_n = n;
// Remove duplicates and count
for(i = 0; i < n; i++) {
for(j = i + 1; j < n; j++) {
if(arr[i] == arr[j]) {
for(k = j; k < n - 1; k++) {
arr[k] = arr[k + 1];
}
n--;
j--;
duplicates_removed++;
}
}
}
printf("\nArray after removing duplicates: ");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
printf("Total duplicates removed: %d\n", duplicates_removed);
printf("Original size: %d, New size: %d\n", original_n, n);
return 0;
}
Frequently Asked Questions
1. How do you remove duplicate elements from an array in C?
You can use nested loops to check for duplicates and shift elements to the left. For sorted arrays, use the two-pointer approach. For unsorted arrays, you can also use a visited array.
2. What is the most efficient way to remove duplicates?
For sorted arrays, the two-pointer approach is the most efficient with O(n) time and O(1) space. For unsorted arrays, consider sorting first and then using the two-pointer approach.
3. Does the program maintain the original order of elements?
Yes, the nested loop method maintains the original order of the first occurrence of each element. The sorted array method also maintains order.
4. What happens if all elements are the same?
If all elements are the same, the array will contain only one element after removing duplicates.
5. Can I use this method for strings?
Yes, the same logic can be applied to arrays of strings using strcmp() instead of == for comparison.
š” Tip: For large unsorted arrays, consider sorting the array first and then removing duplicates using the two-pointer approach for better performance.