C program to print all unique elements in the array
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:
- Input the Array: Read the array elements from the user.
- Identify Unique Elements: Check each element to determine if it is unique.
- Print Unique Elements: Output the unique elements to the console.
We'll discuss two methods to achieve this:
- Using Nested Loops: This method involves comparing each element with every other element in the array.
- 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:
- Input the Array: The user inputs the size of the array and the elements.
- 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.
- 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:
- Input the Array: The user inputs the size of the array and the elements.
- Frequency Array: The program initializes a frequency array to zero. It then iterates through the input array, incrementing the frequency of each element.
- 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