Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
1.1.
Delete Duplicate Elements from the Array Using Nested Loops.
1.2.
Delete Duplicate Elements from the Sorted Array Using User-Defined functions.
1.3.
Delete Duplicate Elements from the Array by creating a new array.
2.
FAQs
3.
Key Takeaways
Last Updated: Mar 27, 2024

C Program to Delete Duplicate Elements from the Array.

Author Gaurav Joshi
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

This article will discuss how to delete duplicate elements from the Array in the 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. But Before going ahead, let us understand what an array is? An array is a collection of similar data type elements stored in a contiguous memory location. An array is defined as the derived data structure that stores primitive data type elements like int, char, float, etc., in contiguous memory.

When the same types of an element occur, again and again, we call them duplicate elements. Next, we will discuss different methods to delete duplicate elements from the Array.

Also see, Tribonacci Series and Short int in C Programming

Delete Duplicate Elements from the Array Using Nested Loops.

This section deletes duplicate elements from the Array using a nested loop. The first FOR loop is for iteration into each index value. The next FOR loop inside the previous one is to iterate on the rest of the Array from the current position to find the next duplicate element. We enter the next FOR loop only in a condition if a duplicate element is present. FOR loop is used to delete duplicate elements found at that position by shifting the following elements by one position to remove the duplicate element from the Array.

Code:-

// Include Header Files
#include<stdio.h>
#include<stdlib.h>

// Define Main function
int main(){

   // Declare int variables and arrays
   int array[25], i, j, k, size;
   
   // Ask user for array size
   printf("Enter the size of the array\n");
   scanf("%d",&size);
   
   // Ask user for elements in the array
   printf("Enter Elements of the array:\n");
   
   // Use for loop to insert each element into the array
   for(i=0;i<size;i++){
      scanf("%d",&array[i]);
   }
   
   // Print the entire array including duplicate one
   printf("Original Array:- \n");
   for(i=0;i<size;i++){
      printf("%d",array[i]);
   }
   
   // Remove duplicate using nested for loops
   for(i=0;i<size;i++){
      
      // For each index iteration search in the rest of the array for its duplicates.
      for(j = i+1; j < size; j++){
          
         // If duplicates found using if condition
         if(array[i] == array[j]){
             
            // Shift all elements by 1 if duplicates are found.
            for(k = j; k < size; k++){
               array[k] = array[k+1];
            }

            // Move j again to the previous position
            j--;
            
            // Reduce the size of the Array by 1
            size--;
         }
      }
   }
   
   // Print the array once deleting all duplicates
   printf("\nNew Array :-\n");
   for(i=0;i<size;i++){
      printf("%d ",array[i]);
   }
}

Output 01:-

Enter the size of the Array
5
Enter Elements of the Array:
5
4
2
5
4
Original Array:- 
5 4 2 5 4 
New Array:-
5 4 2 

Output 02:-

Enter the size of the Array
6
Enter Elements of the Array:
10
20
30
10
10
20
Original Array:- 
10 20 30 10 10 20 
New Array:-
10 20 30 

You can also read about dynamic array in c and C Static Function.

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;     
      
}

Output 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 use 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;
}

Output 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/. 

FAQs

  1. 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.
     
  2. Define Array?
    An array is a  collection of similar data type elements that are stored in a contiguous memory location.
     
  3. 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.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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. If you want to learn more about such content and practice some good quality questions to excel in your preparation. In that case, you can visit our Guided Path in  Coding Ninjas Studio.To be more confident in data structures and algorithms, try out our  DS and Algo Course. All the best for your future endeavours, and Keep Coding.

Recommended Topic: Array in Javascript and reverse a number

Previous article
Program to Swap First and Last Digit Of a Number In C
Next article
C Program For Inserting an Element In an Array
Live masterclass