In this tutorial you will learn about the C Program to Delete Duplicate Elements from an Array and its application with practical example.
C Program to Delete Duplicate Elements from an Array
In this tutorial, we will learn to create a C program that will Delete Duplicate Elements from an Array in C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C programming.
- Conditional Statements in C programming.
- Array in C programming.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declare the variables for the program. 2. Take the size and the elements of the array from the user. 3. Using the for loop to remove the duplicate element of the array. 4. Printing the array. 5. End the program. |
Delete Duplicate Elements from an Array:-
In this program, we will first take the input size of the array. Then we will the elements of the array from the user. After that, we will remove the duplicate elements from the array. At last, we will print the array to the user with the removed elements.
With the help of this program, we can Delete Duplicate Elements from an Array.
Program Code:-
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 |
/* C Program to Delete Duplicate Elements from an Array */ #include <stdio.h> int main() { //Declaring the required variable for the program. int arr[10], i, j, k, Size; //i = it will hold the integer value for the loop. //j = it will hold the integer value for the loop. //k = it will hold the integer value for the loop. //size = it will hold the size of the array. //arr[] = it will hold the integer value for the loop. //Taking the size of the array. printf("\n Please Enter Number of elements in an array : "); scanf("%d", &Size); //Taking the element of the array from the user. printf("\n Please Enter %d elements of an Array \n", Size); for (i = 0; i < Size; i++) { scanf("%d", &arr[i]); } //Searching for the duplicate elements of the program. for (i = 0; i < Size; i++) { for(j = i + 1; j < Size; j++) { if(arr[i] == arr[j]) { for(k = j; k < Size; k++) { arr[k] = arr[k + 1]; } Size--; j--; } } } //Printing the final array after removing the element. printf("\n Final Array after Deleteing Duplicate Array Elements is:\n"); for (i = 0; i < Size; i++) { printf("%d\t", arr[i]); } return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- i = it will hold the integer value.
- j = it will hold the integer value.
- k = it will hold the integer value.
- arr[] = it will hold the integer value.
Taking the input size and the elements of the array.
Delete Duplicate Elements from an Array.
Printing the array.