C program to print all unique elements in the array

Category: C Program

Learn how to write a C program to print all unique elements in an array. Explore methods using nested loops and frequency arrays, with complete code examples and explanations.

Handling arrays and processing their elements is a common task in C programming. One such task is identifying and printing all unique elements in an array. This article will guide you through writing a C program that accomplishes this, along with explanations and sample code.

Steps to Print Unique Elements

To print all unique elements in an array, we can follow these steps:

  1. Input the Array: Read the array elements from the user.
  2. Identify Unique Elements: Check each element to determine if it is unique.
  3. Print Unique Elements: Output the unique elements to the console.

We'll discuss two methods to achieve this:

  1. Using Nested Loops: This method involves comparing each element with every other element in the array.
  2. Using a Frequency Array: This method uses an auxiliary array to keep track of the frequency of each element.

Method 1: Using Nested Loops

#include <stdio.h>

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

    int arr[size];
    printf("Enter %d elements -\n", size);
    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 isUnique = 1;
        for (int j = 0; j < size; j++) {
            if (i != j && arr[i] == arr[j]) {
                isUnique = 0;
                break;
            }
        }
        if (isUnique) {
            printf("%d ", arr[i]);
        }
    }

    return 0;
}

Explanation:

  1. Input the Array: The user inputs the size of the array and the elements.
  2. Identify Unique Elements: The program uses two nested loops. The outer loop iterates through each element, and the inner loop checks if the current element is equal to any other element.
  3. Print Unique Elements: If an element is not equal to any other element, it is considered unique and printed.

Advantages:

  • Simple and easy to understand.
  • No additional memory required apart from the input array.

Disadvantages:

  • Time complexity is O(n^2), making it inefficient for large arrays.

Method 2: Using a Frequency Array

#include <stdio.h>

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

    int arr[size];
    printf("Enter %d elements -\n", size);
    for (int i = 0; i < size; i++) {
        scanf("%d", &arr[i]);
    }

    int freq[100] = {0}; // Assuming the maximum possible value in the array is 99

    for (int i = 0; i < size; i++) {
        freq[arr[i]]++;
    }

    printf("Unique elements in the array are: ");
    for (int i = 0; i < size; i++) {
        if (freq[arr[i]] == 1) {
            printf("%d ", arr[i]);
        }
    }

    return 0;
}

Explanation:

  1. Input the Array: The user inputs the size of the array and the elements.
  2. Frequency Array: The program initializes a frequency array to zero. It then iterates through the input array, incrementing the frequency of each element.
  3. Print Unique Elements: The program iterates through the input array again and prints elements that have a frequency of one.

Advantages:

  • More efficient with a time complexity of O(n).
  • Suitable for larger arrays compared to the nested loop method.

Disadvantages:

  • Requires additional memory for the frequency array.
  • Assumes a known range of possible values in the array.

Output

Enter the size of the array: 10
Enter 10 elements -
1
2
6
4
2
1
8
9
55
88
Unique elements in the array are: 6 4 8 9 55 88

Recommended Posts