Delete Duplicate Elements in Array in C Programming

delete-duplicate-elements-in-array-in-c-programming

Program Description:

Write a Program to Delete Duplicate Elements in Array in C programming language. The program should accept an input array from the user, Input array may contain duplicate elements, The program should remove all duplicate elements from the array and provide the results.

Here is the example input and output of the program.

Example Input and Output:

Input:

Enter desired array size(1-1000): 8

Please enter array elements(8) : 1 2 1 2 1 2 3 3

Output:

Original Array: 1 2 1 2 1 2 3 3

Array (without Duplicates): 1 2 3

The input array 1 2 1 2 1 2 3 3 has few duplicate elements, So the result after removing the duplicate elements is 1 2 3

📢 Related Programs:

This program is part of the Series of Array Practice Programs in C Language

Prerequisites:

It is recommended to have a basic knowledge of the Arrays, and Functions in C language. Please go through the following articles to learn more about them.

Delete Duplicate Elements in Array in C Algorithm:

To delete the duplicate elements in the input array, We are going to create a new array that is going to store distinct elements of the original array. This is equal to deleting all duplicate elements from the array.

So we need to go through the original array element by element and Insert the element into the new array only if the current element is not present in the new array.

Once the above loop is completed, The new array will contain only the distinct elements (i.e without duplicate elements)

In the end, If we want to replace the original array, We can replace the original array elements with the new array elements.

program-delete-duplicates-in-array

Now let’s look at the step-by-step explanation of the delete duplicate elements in the array program.

Delete Duplicate Elements in Array in C Program Explanation:

  1. Declare the numbers array, with the size of 1000. Also, create an array named noDups with 1000 elements. The noDups array is going to store the distinct elements of the numbers array.
  2. Take the desired array size from the user and update the size variable.
  3. Check for the array bounds using the if(size >= 1000 || size <= 0), Display an error message if the size goes beyond the bounds.
  4. Request input from the user for the array’s elements, and update the array.
    • Use a For Loop to iterate over the array and update the numbers[i] with the scanf function
  5. To get the distinct elements of the array, Traverse through the original array (i.e numbers) and Update the noDups array only if the current element( numbers[i]) is not found in the noDups array.
  6. Start a For loop from i=0 to i<size to iterate over the original array.
    • Use isFound variable to check if the element is found in the noDups
      array. Set isFound=0 at the start of each iteration.
    • Create another for loop from j=0 to j<noDupsSize
      • check if the present number number[i] is present in the noDups array.
      • if the number[i] == <code>noDups[j]
        condition is true, then update the isFound=1. and break out of the loop.
    • Check the isFound is still zero(0), If so, Then the number[i] element is not found in the noDups array, So update the noDups array with the number[i]noDups[noDupsSize] = numbers[i];
    • Increment the noDupsSizenoDupsSize++
  7. Once the above two loops are completed, The noDups
    array will not contain any duplicate elements. So we are able to delete duplicate elements from the array.
  8. Display the results. First display the input array by using a For loop, Then display the noDups array using another for loop.

📢We can also replace the elements of the input array( numbers) with the new array( noDups) elements.

Program to Delete Duplicate Elements in Array in C Language:

Let’s convert above algorithm into the C Program, which deletes all duplicate elements from the input array.

Program Output:

Now, We need to compile and Run the Program.

Compile the program using GCC compiler (Any compiler)

$ gcc delete-duplicates.c

The above command generates an executable file named a.out (we are using GCC compiler on ubuntu Linux, If you are using a windows system, you will get .exe file).

Run the Executable file using the following command ( a.out)

Test Case 1: Normal Cases:

delete-duplicate-elements-in-array-program-output

As we can see from the above output, The program is properly removing the duplicate elements.

For example, If the input array is 18 98 34 98 76, Then the program removed the duplicate element 98 and provided the output array as 18 98 34 76.

Test Case 2: Check the Array Bounds:

As excepted, If the given array size is out of bounds, Then the program is displaying an error message which is Invalid Size, Try again.

Related Array Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Delete Duplicate Elements in an Array […]

Leave a Reply