C program to delete all duplicate elements from an array – In this article, we will specify the numerous ways to delete all duplicate elements from an array in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The ways used here for C program delete duplicate elements from an array:
- Using Standard Method
- Using Function
Arrays are nothing but a collection of elements present in a sequential order horizontally. The arrays are a very significant part of C programming. The position of elements in an array is denoted with the help of pointers.
As given in the image above, firstly, you need to enter the size of the given array.
The size of this particular array is 10.
Then, you need to enter the elements of that array.
The elements entered in this array are as follows:
1 1 2 2 3 3 4 4 5 5
As you can see, each element is repeated twice. Hence, after removing the duplicates, the array looks like this:
1 2 3 4 5
Thus, the means to the same in C programming are as follows:
Using Standard Method
- Read the array size and store it into the variable n.
2) Scanf reads the entered elements and stores the elements in the array using for loop for(i=0;i<n;i++).
3) To find duplicate elements in the array
for loop iterates from i=0 to i<n
a) if a[i]!=-1
compare each element of the string with a[i] using for loop for(j=i+1;j<n;j++).if any element is equal to a[i] then increase the c value by 1 and put -1 in place of duplicate value i.e a[j]. Repeat until j<n.
b) If a[i]!=-1 place the a[i] into the array a[k++].Here we initializing the non-duplicate elements into the string a[k].
Repeat a,b steps until i<n.
4) Print the elements after deleting duplicates in array as
for loop iterates from i=0 to i<size of the array-number of duplicate elements
printf(“%d”,a[i]).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
#include <stdio.h> #include <conio.h> int main() { int a[10000],i,j,n,k=0,c=0 ; printf("Enter size of the array : "); scanf("%d", &n); printf("Enter elements in array : "); for(i=0; i<n; i++) { scanf("%d",&a[i]); } for(i=0; i<n; i++) { if(a[i]!=-1) { for(j=i+1; j<n; j++) { if(a[i]==a[j]) { c++; a[j]=-1; } } } if(a[i]!=-1) { a[k++]=a[i]; } } printf("elements after deleting duplicates in array :\n"); for(i=0; i<n-c; i++) { printf("%d ",a[i]); } return 0; } |
Output:
1 2 3 |
Enter size of the array : 10 Enter elements in array : 1 1 2 2 3 3 4 4 5 5 elements after deleting duplicates in array : 1 2 3 4 5 |
Using Function
- The main() function calls the count() function by passing array a, size of the array as arguments.
2) The count function finds the number of duplicate elements in the given array and returns the resultant array size to main() function as
for loop iterates from i=0 to i<array size
a) if a[i]!=-1
compare each element with a[i] using for loop for(j=i+1;j<n;j++).If any element match with a[i] then increase the count value by 1 and place -1 at a[j] which is duplicate element.Repeat until j<n.
b) If a[i]!=-1 store the a[i] into a[k++] for each iteration of i.
c) The count() function returns the n-c to the main() funtion and n-c initialized to n.
3) The main() function calls the print() function passing array,n as arguments.Then print() prints the elements after deleting duplicates in the array as printf(“%d”,a[i]) using for loop for(i=0;i<n;i++).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
#include <stdio.h> #include <conio.h> int count(int *a,int n) { int i,c=0,j,k=0;; for(i=0; i<n; i++) { if(a[i]!=-1) { for(j=i+1; j<n; j++) { if(a[i]==a[j]) { c++; a[j]=-1; } } } if(a[i]!=-1) { a[k++]=a[i]; } } return n-c; } print(int *a,int n) { int i; for(i=0; i<n; i++) { printf("%d ",a[i]); } } int main() { int a[10000],b[10000],i,n; printf("Enter size of the array : "); scanf("%d", &n); printf("Enter elements in array : "); for(i=0; i<n; i++) { scanf("%d",&a[i]); } n=count(a,n); printf("elements after deleting duplicates in array :\n"); print(a,n); return 0; } |
Output:
1 2 3 4 |
Enter size of the array: 10 Enter elements in array : 1 2 3 1 2 3 1 2 4 5 elements after deleting duplicates in the array : 1 2 3 4 5 |