write a c program which deletes the duplicate elements of an array.
C program, remove duplicates, array, delete duplicates, duplicate elements
"Write a C program which deletes the duplicate elements of an array"
means to write a program in the C programming language that takes an array as input, finds any duplicate elements in the array, and removes them so that only the unique elements remain in the array.
For example, suppose we have an array [1, 2, 3, 2, 4, 5, 1].
The program should remove the duplicate elements 2
and 1,
and leave only the distinct elements [3, 4, 5, 2]
in the array.
The program should be able to handle arrays of any size, and should work by comparing each element of the array to every other element to determine if there are any duplicates. If a duplicate is found, it should be removed from the array.
Once the program has removed all duplicate elements from the array, it should print the resulting array to the console or output it to a file or another data structure.
Original: [ A, C, B, D,A, B,E, D, B, C]
Remove duplicate results: D,E, A,B, C
C Program to Delete Duplicate Elements of an Array
#include<stdio.h>
#include<string.h>
void removeDuplicates(char arr[], int n) {
int i, j, k;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n;) {
if (arr[j] == arr[i]) {
for (k = j; k < n; k++) {
arr[k] = arr[k + 1];
}
n--;
} else {
j++;
}
}
}
}
int main() {
char arr[] = "Hello World!";
int n = strlen(arr);
int i;
printf("Original array: %s\n", arr);
removeDuplicates(arr, n);
printf("Array after removing duplicates: %s\n", arr);
return 0;
}
The output of the C program you provided is:
Original array: Hello World!
Array after removing duplicates: Hlo World!
The program works by iterating over the array and comparing each element to the next element. If the two elements are the same, the program removes the second element. The program then repeats this process until it reaches the end of the array.
In this program, the removeDuplicates()
function takes a character array arr
and its size n
as inputs. The function iterates over each element in the array and compares it to all subsequent elements in the array. If a duplicate is found, the function shifts all subsequent elements to the left by one position and decrements the size of the array by 1.
The main() function initializes a character array with duplicate elements, prints the original array, calls the removeDuplicates()
function to remove duplicates, and prints the resulting array.
Note that this program modifies the original array. If you want to preserve the original array and create a new array without duplicates, you'll need to modify the program accordingly
Further Reading:
write a program to find the largest elements of a given array with an index.
write a c program to find the second largest elements of a given array.
write a program to find the first largest and second-largest elements of a given array.
Write a c program find largest elements in this array number or index
Write a C program to find out the second largest and second smallest elements of any unsorted array without using any shorting Technique
write a program to find the smallest elements of a given array with an index.
write a program to find the first largest and first smallest elements of a given array.
write a program to find the second smallest elements of a given array.
write a program to find the second largest and second smallest elements of a given array.
write a program to find the first smallest and second smallest elements of a given array.
Enroll Now:
[ C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"
Contact Us:
- For any inquiries, please email us at [info@electro4u.net].
- Follow us on insta [ electro4u_offical_ ] for updates and tips.