Delete Duplicate Elements from the Sorted Array Using User-Defined Functions
This section deletes duplicate elements from the Array using the delete_duplicate_element function. The function asks for size and the Array. We create a new_array for the temporary purpose of filling only the unique element. We traverse each element of the original array using a for loop and insert only those elements in the new_array if the next element is not equal to the current one. This way, we store all elements only once. We copy the elements from the new_array to the original Array and return the new_size.
Code:-
// Include header files
#include <stdio.h>
// Define delete_duplicate_elements
int delete_duplicate_element( int array[], int size)
{
// Check for array size if equal to 0 or 1 return size. As no duplicates could be there for a empty array and an array with 1 element
if (size == 0 || size == 1) {
return size;
}
// Creating a new_array to save each element at once only
int new_array[size];
// Declare index,new_size for teration and reference
int index, new_size = 0;
// iterate on each element of array
for (index = 0; index < size - 1; index++)
{
// Insert element into new_array only which are not equal to following element
if (array[index] != array[index + 1]){
new_array[new_size] = array[index];
new_size++;
}
}
// Insert the last element
new_array[new_size] = array[size - 1];
new_size++;
// Insert new_array element into old array
for (index = 0; index < new_size; index++)
{
// All element of new_array copied into original array
array[index] = new_array[index];
}
// Return the new array size
return new_size;
}
// Define main function
int main ()
{
// Declare array size
int size;
// Ask user for array size
printf (" Enter the Array Size :- \n");
scanf (" %d", &size);
// Declare array of length size and int index variable for iteration
int array[size], index;
// Ask user for the elements
printf (" Enter the elements:- \n ");
// Use for loop to insert element into the array one by one
for ( index = 0; index < size; index++)
{
scanf (" %d", &array[index]);
}
// Print original array
printf (" \n Original Array :- \n");
for ( index = 0; index < size; index++)
{
// Print array element present at index for each iteration
printf (" %d", array[index]);
}
// Call delete_dupliacte_function which returns new_size collect in size variable
size = delete_duplicate_element (array, size);
// print new array formed after deleting duplicates
printf (" \n New Array :- \n ");
for ( index = 0; index < size; index++)
{
printf (" %d", array[index]);
}
// Return from the main function
return 0;
}

You can also try this code with Online C Compiler
Run CodeOutput 01:-
Enter the Array Size :-
5
Enter the elements:-
10
10
20
20
30
Original Array :-
10 10 20 20 30
New Array :-
10 20 30
Output 02:-
Enter the Array Size :-
5
Enter the elements:-
10
10
10
10
10
Original Array :-
10 10 10 10 10
New Array :-
10
Delete Duplicate Elements from the Array by creating a new array
This section deletes duplicate elements from the Array by creating a new array that carries elements only once. We ask for array size from the user and input used for declaring both original Arrays as well as new_array. We create a new array to fill only the unique element. We traverse each element of the original Array using a FOR loop and insert only those elements in the new_array if the nested FOR loop iterated over the entire new_arrray, which means we found no duplicate if the element at the current index of the original Array is a duplicate. We use the BREAK statement for incomplete iteration. This way, we store all elements only once.
Code:-
// Include header files
#include <stdio.h>
// Define main function
int main()
{
// Declare an int variable like size,new_size,index,size.
int new_size = 0,index, new_index, size;
// Ask the user for the array size.
printf("\n Enter size of the Array :- \n");
scanf("%d", &size);
// Declare array and new_array
int array[size],new_array[size];
// Ask user for input elements
printf("\n Enter elements of the Array :- \n");
// Insert element one by one in each iteration into the array
for (index = 0; index < size; index++)
{
scanf("%d", &array[index]);
}
// Display the original array i.e, array before removing duplicates
printf("\nOriginal Array :- \n");
for (index = 0;index < size; index++)
{
printf("%d ",array[index]);
}
// Iterate on each index of the original array
for (index = 0; index < size; index++)
{
// for each index iterate on new_array to check if element is duplicate or not
for(new_index = 0; new_index < new_size; new_index++)
{
// Element found in new array i.e, it's a duplicate break
if(array[index] == new_array[new_index]) {
break;
}
}
// We enter the loop only if entire new_array is traversed and no duplicacy found
if(new_index == new_size) {
new_array[new_size] = array[index];
new_size++;
}
}
// Display the new_array.
printf("\nNew Array :- \n");
for (index = 0; index < new_size; index++) {
printf("%d ", new_array[index]);
}
return 0;
}

You can also try this code with Online C Compiler
Run CodeOutput 01:-
Enter the size of the Array:-
5
Enter elements of the Array:-
70
80
70
70
80
Original Array:-
70 80 70 70 80
New Array:-
70 80
Output 02:-
Enter the size of the Array:-
6
Enter elements of the Array:-
10
10
10
20
20
20
Original Array:-
10 10 10 20 20 20
New Array:-
10 20
You can practice it on C online compiler for better understanding/.
Frequently Asked Questions
How to delete duplicate elements from the Array in C language?
There are different ways in C language through which a user could delete duplicate elements from the Array or remove those duplicate elements. We can delete Duplicate Elements from the Array Using Nested Loops, using pointers, using a user-defined function and many ways.
Define Array.
An array is a collection of similar data type elements that are stored in a contiguous memory location.
What is the data type of an array?
An array is defined as the derived data type that stores primitive data type elements like int, char, float, etc., in contiguous memory.
Key Takeaways
In the article, we have discussed the different ways to delete duplicate elements from the Array in the C language.
We hope this blog has helped you enhance your knowledge regarding the C programming language.
Recommended Topic: