PART A · PRESCRIBED EXERCISE 07
Remove duplicate array elements
Understand the algorithm. Run the code.
Explain the result with confidence.
LAB A07 · 01
Understand the task.
Aim: Remove duplicate array elements.
Duplicate removal. Duplicate removal keeps one occurrence of each distinct array value. This algorithm preserves the order in which values first appear.
Review the related theory in Unit 3.
LAB A07 · 02
Know what goes in and what comes out.
First n from 1 to 20, then n integers from -100 to 100. The output preserves first-occurrence order.
Source programs read the documented input directly, without prompt text, so the same sample works in the embedded compiler’s STDIN field.
LAB A07 · 03
Plan the steps before writing code.
- Read the array; set used, the retained-element count, to zero.
- Visit each original element at index i.
- Search only the retained prefix a[0] through a[used - 1].
- If the value is absent, store it at a[used] and increment used.
- Print the retained prefix.
LAB A07 · 04
One complete program. Ready to run.
#include <stdio.h>
#define MAX 20
int main(void)
{
int a[MAX], n, i, j, used = 0, found;
if (scanf("%d", &n) != 1 || n < 1 || n > MAX) {
puts("Enter a count from 1 to 20.");
return 1;
}
for (i = 0; i < n; i++) {
if (scanf("%d", &a[i]) != 1 || a[i] < -100 || a[i] > 100) {
puts("Enter each value from -100 to 100.");
return 1;
}
}
for (i = 0; i < n; i++) {
found = 0;
for (j = 0; j < used; j++) {
if (a[j] == a[i]) { found = 1; break; }
}
if (!found) a[used++] = a[i];
}
printf("Unique values:");
for (i = 0; i < used; i++) printf(" %d", a[i]);
putchar('\n');
return 0;
}
Unique values: 4 2 7 9
Compile on your own computer
gcc A07-remove-duplicates.c -o programRun the generated program and enter the sample input. The examples use standard C90-compatible syntax and explicit function declarations. For these learning programs, enter numeric values representable in the stated C types and within the documented ranges.
LAB A07 · 05
Connect each statement to its purpose.
- The retained prefix contains unique values in their original first-occurrence order.
- used never exceeds i + 1. A write to a[used] is at or behind the element being processed, so it does not overwrite future unread elements.
- No sorting is performed. Sorting-based removal would change the ordering unless extra work restored it.
- The number of retained values can be smaller than n; the original array capacity remains unchanged.
LAB A07 · 06
Follow the sample calculation.
| Step or component | What happens |
|---|---|
| 4 | Not retained yet → [4] |
| 2 | New → [4, 2] |
| 4 | Already retained → [4, 2] |
| 7, 2, 9, 7, 4 | Retain only new values → [4, 2, 7, 9] |
LAB A07 · 07
Check more than one input.
| Try this input or case | Expected behaviour |
|---|---|
| 5: 7 7 7 7 7 | Unique values: 7 |
| 4: 1 2 3 4 | All four are retained. |
| 5: -1 0 -1 2 0 | Unique values: -1 0 2 |
Time and storage
O(n²) worst-case time from repeated prefix searches; O(1) extra working space because the array is compacted in place.
Start with the sample, then test a boundary and a case that changes the result. A correct sample alone does not establish that every input is handled correctly.
LAB A07 · 08
Use the real C compiler.
Choose an example and load it into the embedded C compiler. Select I/O inside the compiler, check the sample STDIN, and press Run. Change the input or edit the source to explore a different case.
Load the compiler when you’re ready to practise.
Your C workspace
Edit real C code and see real compiler output.
Compiler provided by OneCompiler. Opening the editor connects to that service; code and input you run are processed there. If the embed is unavailable, open the C compiler in a new tab and paste the source below.
View / copy the selected source
#include <stdio.h>
#define MAX 20
int main(void)
{
int a[MAX], n, i, j, used = 0, found;
if (scanf("%d", &n) != 1 || n < 1 || n > MAX) {
puts("Enter a count from 1 to 20.");
return 1;
}
for (i = 0; i < n; i++) {
if (scanf("%d", &a[i]) != 1 || a[i] < -100 || a[i] > 100) {
puts("Enter each value from -100 to 100.");
return 1;
}
}
for (i = 0; i < n; i++) {
found = 0;
for (j = 0; j < used; j++) {
if (a[j] == a[i]) { found = 1; break; }
}
if (!found) a[used++] = a[i];
}
printf("Unique values:");
for (i = 0; i < used; i++) printf(" %d", a[i]);
putchar('\n');
return 0;
}
LAB A07 · 09
Explain why the program works.
Does this remove duplicates only when adjacent?
No. Every element is checked against all previously retained distinct values.
Why stop searching at used rather than n?
Only the retained prefix contains the unique values already accepted.
Does the declared array become smaller?
No. used describes the meaningful output count; the allocated capacity is unchanged.
LAB A07 · 10
Build the next skill.
Read the viva answers, try the extra cases, and return to the lab index to mark this exercise as practised.