Count Number of Unique Elements in Array in C Language

Program-to-Count-Number-of-Unique-Elements-in-Array-in-C-Language

Program Description:

We have looked at the Program to Reverse Array Elements using Recursion in our earlier article, In today’s article, We will look the program to Count the Number of Unique Elements in Array in C programming language. We should not use extra array.

Prerequisites:

We need to know the C Arrays and Functions. Please go through the below posts to learn about them.

Let’s examine what the program’s input and output should look like.

Example Input and Output:

Input:

Enter desired array size(1-1000): 5

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

Output:

Original Array: 1 2 3 3 2

Total number of unique elements in array is : 3

From the above output, The input array contains 5 elements but only 3 are unique elements or Distinct elements.

📢 This Program is Part of the Array Practice Programs Series.

Count Number of Unique Elements in Array in C Explanation/Algorithm:

  1. Start the program by declaring an array, i.e numbers[1000] array. Also, declare other supporting variables.
  2. Initialize the nDistinct variable with zero(0). This nDistinct will hold the number of unique elements in the array.
  3. Prompt the user for the desired size of the array and store it in size variable.
  4. Check for array bounds and display an error message if the size is out of bounds. – if(size >= 1000 || size <= 0)
  5. Request input from the user for the array’s elements, and update the numbers array. Use a For Loop to iterate over the elements and update them.
  6. Display the original array on the screen using a For loop.
  7. Now, To Count the Number of Unique Elements in an Array, We need to iterate over the numbers array and increment the nDistinct variable if there is no duplicate element found in the array. So we need to use two For Loops.
  8. Create a For Loop (Outer Loop) to iterate over the numbers array. Start from i=0 and go till i>size
    • Create another for loop (Inner Loop), The inner loop will go from the j=0 to j<i
      • At Each Iteration, Check if the numbers[i] == numbers[j]. If this condition is true, Then we found a Duplicate, So break the inner loop.
    • Inside of the Outer Loop, Check if the i == j, If it is true, Which means we reached the end of the array but didn’t find the duplicate element for the current element ( numbers[i]). So we can increase the nDistinct variable. So increment the nDistinct variable using Increment Operator.
  9. Once the above two For Loops are completed, The nDistinct variable contains the total number of Unique elements in the array.
  10. Display the results on the standard Output using printf function.

Program to Count Number of Unique Elements in Array in C Language:

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

Program Output:

Compile and Run the Program.

program-to-count-number-of-unique-numbers-in-array

Let’s try few more examples

As you can see from the above output, The program is properly generating the count of unique or distinct numbers.

For example, When we entered 99 88 44 55 88 99 88 22 33 11 as the input array, The program generated output as 7, which is the number of unique elements in the array.

Let’s check few corner cases

From the above output, We can see the program is generating the an error message if the array size is not valid.

Count Number of Unique Elements in Array using functions in C:

It is always good idea to divide the program into the functions, Functions help to increase the debugging, readability and encourages modularity, So let’s modify the above program and divide it in to the few functions, Where each function does a specific task. Then call the defined functions from the main() function.

Here is the modified version of the above program, We have defined three functions(except main() function) in the following program. Look at the main() function and how clean it became.

In the above program, We have defined three extra functions, which are

  1. The read() function – void read(int numbers[], int size)
    • This function takes two formal arguments. The numbers[] array and the array size
    • The read() takes the user input and updates the array elements.
  2. The display() function – void display(int numbers[], int size)
    • This function also takes two formal arguments, which are numbers[] array and its size
    • The display() function is used to display the array on the standard output (console).
  3. The countDistinct() Function – int countDistinct(int numbers[], int size)
    • The countDistinct() function also takes two arguments similar to the read() and display() functions.
    • This function goes through the array elements using two for loops and calculates the Distinct Elements in the array.
    • It returns the total number of distinct elements back to the caller.

We Called the above functions from the main() function and displayed the results on the console.

Program Output:

Let’s compile and run the program using GCC compiler (Any compiler)

count-unique-numbers-in-array-using-functions-in-c-language

Check negative cases

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

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 Count Number of Unique Elements in Array […]

  2. […] C Program to Count Number of Unique Elements in Array […]

Leave a Reply