Program to Find Unique Elements in Array in C Language

program-to-find-Unique-Elements-in-Array-in-C-Language

Program Description:

Write a C Program to Find Unique Elements in Array in C programming language. The program accepts a input array from the user and finds the unique elements or distinct elements in the given array.

Example Input and Output of the Program:

Input:

Enter desired array size(1-1000): 8

Please enter array elements(8) : 11 77 88 22 88 77 11 22

Output:

Original Array: 11 77 88 22 88 77 11 22

Array (Unique Elements): 11 77 88 22

Note that the output only contains the distinct element or Unique elements in the given array.

Prerequisites:

To better understand the following program, Please go through the C Arrays, Functions in C, and How to Pass Arrays to Functions Articles.

algorithm-to-get-unique-elements-in-array

Algorithm to Find the Unique Elements in Array in C language:

To find the unique elements in the array, We are going to create a new array which is going to store only unique elements from the original array.

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

Once the above loop is completed, The new array will contain only the unique elements.

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

📢 We are using extra space O(n) in this example.

Now let’s look at the step by step explanation of the program.

Unique Elements in Array in C Program Explanation:

  1. Declare the numbers array, with the size of 1000. Also, create an array named unique with 1000 elements. The unique array is going to store the unique 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 unique elements of the array, Traverse through the original array (i.e numbers) and Update the unique array only if the present element not found in the unique array.
  6. Start a For loop from i=0 to i<size to iterate over the original array.
    • use isExists element to check if the element is found in the unique array. Set isExists=0 at the start of each iteration.
    • Create another for loop from j=0 to j<uniqueSize
      • check if the present number number[i] is present in the unique array.
      • if the number[i] == unique[j] condition is true, then update the isExists=1. and break out of the loop.
    • Check the isExists is still zero(0), If so, Then the number[i] is not found in the unique array, So update the unique array with the number[i]unique[uniqueSize] = numbers[i];
    • Increment the uniqueSizeuniqueSize++
  7. Once the above two loops are completed, The unique array will contain the Unique elements in Array.
  8. Display the results. First of display the input array by using a For loop, Then display the unique array using another for loop.

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

Program to Get Unique Elements in Array in C:

Let’s convert the above algorithm into the C program.

Program Output:

Compile the program using the your favorite compiler. We are using the GCC compiler in this example.

gcc unique-elements.c

The above command, Generates an executable file named a.out. Run the executable file using the ./a.out command.

Test Case 1: Let’s the positive test cases

get-unique-elements-from-an-array-program-output

As we can see from the above output, The program is generating the excepted results.

For example, If the input array is Please enter array elements(6) : 1 1 1 1 1 1, Then the unique elements in the array are Array (Unique Elements): 1.

Test Case 2: Negative test cases

The max array size is 1000, So if user enters size beyond the bounds of the array, Then display an error message ( Invalid Size, Try again).

⚠️ Please change the 1000 value in the array declaration, If you want to use the larger arrays.

Additionally, We can also use a Macro or Const variable to hold the value of the 1000. For the brevity, we have directly used the 1000.

Excersice:

Rewrite the above program to use the user defined functions for

  • Reading the user input and updating the array
  • Displaying the Array elements
  • getUnique function to get the unique elements from the array.

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. […] C Program to Find Unique Elements in an Array […]

  2. […] C Program to Find Unique Elements in an Array […]

Leave a Reply