Delete Duplicates from Sorted Array in C Language

Delete-Duplicates-from-Sorted-Array-in-C-Language

Delete Duplicates from Sorted Array in C Program Description:

We have written the count number of unique elements program in our earlier article, In today’s article, We are going to look at the program to Delete Duplicates from Sorted Array in C programming language. The program should accept a Sorted Array from the user, The input array may contain duplicate elements but the array should be in sorted order, So the program will delete all duplicate elements from the given array.

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

Excepted Input and Output of the Program:

Input:

Enter desired array size(1-1000): 8

Please enter array elements(8) in ascending order : 1 1 1 1 2 3 4 4

Output:

Original Array (Sorted): 1 1 1 1 2 3 4 4

Array After removing Duplicates: 1 2 3 4

If you observe the above input and output, The user provided a sorted array 1 1 1 1 2 3 4 4, the array has few duplicate and the program removed all duplicate elements and returned the result as 1 2 3 4 array.

Program Prerequisites:

We are going to use arrays and functions, So it is a good idea to know about Arrays in C, Functions in C, and Passing Arrays to Functions

Let’s look at the step-by-step explanation of the Delete Duplicate elements from an array C program.

Delete Duplicates from Sorted Array in C Explanation:

  1. Declare an array named values with the size of 1000. ( Change the 1000, if you want to use large arrays). We can use a Macro or Const Integer to hold the 1000 but we are directly using it.
  2. Take the desired array size from the user and update the size variable.
  3. Check if the size is out of bounds using if(size >= 1000 || size <= 0) condition and display an error message if the size is out of bounds.
  4. Now, We need to prompt the user to provide the input array. Update the input array elements with user-provided values.
    • Use a For Loop to iterate over the array and update the values[i] element.
  5. Print the Original Array i.e numbers array on the console for comparison. ( Use another for loop and traverse the array and print elements)
  6. Now we need to delete the duplicate elements from the sorted array, As the array is already sorted, The similar elements(numbers) will be adjacent to each other. So if the present element and the next element in the array are equal, Then skip the element and go to the next iteration
    • Initialize variables i=0, j=0. Start a for loop from i=0 to i<size-1, At each iteration,
      • Check if the values[i] is not equal to the values[i+1], If both are not equal, Then Only update the array values array.
      • So if (values[i] != values[i+1]) is true, Then update values[j++] = values[i];
    • Continue the loop till the i reaches size-2.
    • Finally, update the values[j++] = values[size-1];
  7. Once the above steps are completed, Then the values array will only contain the distinct or unique elements.
  8. Display the resultant array on the console using another for loop.

Program to Delete Duplicates from Sorted Array in C Language:

Here is the C Program to Delete Duplicates from a sorted array.

Program Output:

Let’s compile and Run the program using your favorite compiler (we are using GCC compiler on ubuntu linux)

Compile the Program

$ gcc delete-dups-sorted-arr.c

Run the executable file

Test 1: Normal Cases.

delete-duplicate-elements-from-sorted-array-in-c-program-output

The program is properly removing the duplicate elements from the sorted array.

For example, When we provided the 6 6 7 7 7 as the input array, The program removed all duplicate elements and returned the 6 7 Array as output.

Test 2: Negative Cases

As we can see from the above output, The program is providing an error message(i.e Invalid Size, Try again) if the user enters the wrong size.

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

2 Responses

  1. […] our previous article, We have looked at the program to Delete All duplicate elements in a sorted array. In today’s article, We are going to write a Program to Merge Two Arrays in C programming […]

  2. […] C Program to Delete Duplicate Elements from Sorted Array […]

Leave a Reply