Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › ARRAY BASICS
A unique value occurs exactly once in the array. A duplicate value occurs more than once. For [4, 2, 4, 7, 2, 2, 9], the value 7 is unique because it occurs once, and 9 is unique for the same reason. The values 4 and 2 are duplicate values because they occur more than once.
The count is about distinct values, not about how many positions hold those values. There are four distinct values in this array: 4, 2, 7, and 9. Two of those groups are unique and two are duplicate, so the expected result is uniqueCount = 2 and duplicateCount = 2.
What are uniqueCount and duplicateCount for [4, 2, 4, 7, 2, 2, 9]?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Before classifying a[i], scan the earlier indices from 0 through i - 1. If one of those positions contains the same value, the current position is a later occurrence, so skip it. This makes index 0 the only position that classifies value 4, and index 1 the only position that classifies value 2.
bool seenEarlier = false;
for (int j = 0; j < i; j++) {
if (a[j] == a[i]) {
seenEarlier = true;
break;
}
}
if (seenEarlier) {
continue;
}For the running array, index 2 contains 4, but index 0 already contains 4, so index 2 is skipped. Indices 4 and 5 contain 2, but index 1 already contains 2, so both are skipped. Indices 3 and 6 contain values that have not appeared earlier, so each remains eligible for classification.
When a position is the first occurrence of its value, scan all seven positions and count how often that value appears. At index 0, the value is 4 and its frequency is 2, so duplicateCount increases. At index 1, the value is 2 and its frequency is 3, so duplicateCount increases again. A frequency of 1 would increase uniqueCount instead.
int frequency = 0;
for (int j = 0; j < n; j++) {
if (a[j] == a[i]) {
frequency++;
}
}
if (frequency == 1) {
uniqueCount++;
} else {
duplicateCount++;
}At index 4, where a[4] is 2, enter whether the value was seen earlier and whether either counter changes.
a[4] = 2Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The complete traversal examines each position, checks whether its value appeared earlier, and runs a frequency scan only for first occurrences. Index 0 classifies 4 as a duplicate value because its frequency is 2. Index 1 classifies 2 as a duplicate value because its frequency is 3. Indices 2, 4, and 5 are skipped because their values were already classified.
Index 3 is the first occurrence of 7, and its frequency is 1, so uniqueCount becomes 1. Index 6 is the first occurrence of 9, and its frequency is 1, so uniqueCount becomes 2. The duplicate counter became 2 from values 4 and 2, giving the final result uniqueCount = 2 and duplicateCount = 2.
for (int i = 0; i < n; i++) {
bool seenEarlier = false;
for (int j = 0; j < i; j++) {
if (a[j] == a[i]) {
seenEarlier = true;
break;
}
}
if (seenEarlier) {
continue;
}
int frequency = 0;
for (int j = 0; j < n; j++) {
if (a[j] == a[i]) {
frequency++;
}
}
if (frequency == 1) {
uniqueCount++;
} else {
duplicateCount++;
}
}This approach uses constant extra storage because it keeps only counters, a frequency variable, and a few loop variables. It does more comparisons because it may scan earlier positions and then all positions for each first occurrence. Its time complexity is O(n^2), which is the cost of solving the classification with nested array scans instead of another collection.