C Program to Find all Unique Elements in an Array

In this article, we will write a C program to find all the unique elements in an array. Given an array of size N, our task is to find out all the elements which are not repeated i.e. they are unique inside the array.

Sample Example:

Input: arr[] = {1, 2, 2, 3, 4, 5, 5, 6}

Output:
Unique Elements are: 1, 3, 4, 6

Explanation:
Elements 2 and 5 are repeated twice, that's why they are not considered as unique

An element is said to be unique in an array if it appears only once. In other words, there are no duplicate occurrences of that element within the array.

To find out all the unique elements in an array, we have to basically run a for or while loop over the entire array and check the count of every element from start to end.

If the count of the element in the entire array is exactly one, it is unique, otherwise, it’s not.

The solution is to use two nested for loops. The outer for loop picks each element of the array one by one, and the inner for loop checks its count by comparing the current element of the outer loop with each element of the inner loop.

If the current element of the outer loop becomes equal to the current element of the inner loop, we increment the count by 1.

Every time the inner loop finishes, we check the count of the current element picked by the outer for loop. If the count is exactly 1, we print the element as a part of the output.

The following C program shows how you can find all unique elements in an array:

// C Program to find all unique elements of an array

#include <stdio.h>
#include <conio.h>

int main() {

    int arr[100], size;

    printf("Enter the size of the array: ");
    scanf("%d", &size);

    printf("Enter the Elements of the array: ");
    for (int i = 0; i < size; i++) {
        scanf("%d", &arr[i]);
    }

    printf("Unique Elements in the array are: ");

    for (int i = 0; i < size; i++) {
        int count = 0;  // Set count to 0
        for (int j = 0; j < size; j++) {
            if (arr[i] == arr[j]) {
                count = count + 1;  // Increment count by 1
            }
        }

        // Check if the element appears only once
        if (count == 1) {
            printf("%d ", arr[i]);  // Print the unique element
        }
    }

    return 0;

}

Output:

RUN 1:
Enter the size of the array: 7
Enter the Elements of the array: 1 2 2 3 4 5 5
Unique Elements in the array are: 1 3 4

RUN 2:
Enter the size of the array: 7
Enter the Elements of the array: 1 1 2 3 4 5 6
Unique Elements in the array are: 2 3 4 5 6

The time complexity of this program is O(n2) where n is the size of the array.

I hope you will find this article helpful. Thanks for reading.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts

Leave a Comment