C Program to find all unique elements in an array
Learn how to find all array elements that occur exactly once in C, with validated source code, examples, a dry run, edge cases, and complexity analysis.
In this problem, a unique element is a value that occurs exactly once in the array. To find all unique elements, count how many times each value appears and print it only when its occurrence count is 1.
For example:
Array: 1 2 6 -4 2 1 8 9 -4 88
Unique elements: 6 8 9 88
The values 1, 2, and -4 are not unique because each occurs twice.
C Program to Find All Unique Elements in an Array
#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
int array[MAX_SIZE];
int size;
int foundUnique = 0;
printf("Enter the number of elements: ");
if (scanf("%d", &size) != 1 || size < 1 || size > MAX_SIZE) {
printf("Please enter a size between 1 and %d.\n", MAX_SIZE);
return 1;
}
printf("Enter %d elements:\n", size);
for (int index = 0; index < size; index++) {
if (scanf("%d", &array[index]) != 1) {
printf("Invalid array element.\n");
return 1;
}
}
printf("Unique elements: ");
for (int current = 0; current < size; current++) {
int occurrenceCount = 0;
for (int index = 0; index < size; index++) {
if (array[current] == array[index]) {
occurrenceCount++;
}
}
if (occurrenceCount == 1) {
printf("%d ", array[current]);
foundUnique = 1;
}
}
if (!foundUnique) {
printf("None");
}
printf("\n");
return 0;
}
Sample Output
Enter the number of elements: 10
Enter 10 elements:
1 2 6 -4 2 1 8 9 -4 88
Unique elements: 6 8 9 88
The program prints unique values in the same order in which they appear in the original array.
How the Program Works
- The program validates the size and reads the array.
- The outer loop selects one element at a time using
current. occurrenceCountis reset to0for each selected element.- The inner loop compares the selected value with every array element.
- Each equality increases
occurrenceCount. - A final count of
1means the selected value occurs only once, so it is printed. foundUniquerecords whether at least one unique element was found.
Here is a compact dry run:
| Selected value | Occurrence count | Unique? | Action |
|---|---|---|---|
| 1 | 2 | No | Skip |
| 2 | 2 | No | Skip |
| 6 | 1 | Yes | Print 6 |
| -4 | 2 | No | Skip |
| 2 | 2 | No | Skip |
| 1 | 2 | No | Skip |
| 8 | 1 | Yes | Print 8 |
| 9 | 1 | Yes | Print 9 |
| -4 | 2 | No | Skip |
| 88 | 1 | Yes | Print 88 |
Unique Elements Versus Distinct Elements
The words unique and distinct are sometimes used interchangeably, but they can describe different tasks:
- A unique element occurs exactly once.
- A distinct-element list prints each different value once, even when that value is repeated.
For the array 2, 2, 3, 4, 4:
Unique elements: 3
Distinct elements: 2 3 4
This program follows the “occurs exactly once” definition.
Why Compare with Every Element?
The array is not assumed to be sorted, and its values can be positive, negative, or zero. Comparing the current value with every element gives its complete frequency without requiring a restricted value range or another array.
The selected element also matches itself once. Therefore, a count of exactly 1 proves that no other array position contains the same value.
What If Every Element Is Repeated?
If no value occurs exactly once, foundUnique remains 0 and the program prints None:
Enter the number of elements: 6
Enter 6 elements:
5 2 5 8 2 8
Unique elements: None
What If Every Element Is Unique?
When every value appears once, the program prints the complete array:
Array: 4 -1 0 12
Unique elements: 4 -1 0 12
A one-element array also contains one unique element because that value occurs exactly once.
Alternative Approaches
A frequency array can reduce the running time when all input values fall within a small, known range. However, directly using a value as an index is unsafe for negative numbers and can waste a large amount of memory when the range is wide.
Another approach is to sort a copy of the array and compare neighboring elements. That can achieve O(n log n) time with an efficient sort, but sorting the original array would change its order. The nested-loop solution is simple, preserves the order, accepts any int value, and requires no auxiliary collection.
Time and Space Complexity
- Time complexity:
O(n²)because each of thenelements is compared with allnelements. - Extra space complexity:
O(1)because the search uses only counters, loop variables, and a flag beyond the input array.
Common Mistakes
- Printing a value when its count is greater than one; unique values must have a count of exactly
1. - Forgetting to reset
occurrenceCountbefore checking the next element. - Comparing only with later elements, which can misclassify the final copy of a repeated value as unique.
- Using array values as frequency-array indices without handling negative or out-of-range values.
- Confusing unique elements with a list of distinct values.
- Failing to handle the case where no unique elements exist.
By counting the complete frequency of each value and printing only values with a frequency of one, the program finds every unique element while preserving the original array order.