C Program to Find Common Array Elements
C Program to Find Common Array Elements
A class stores five marks in their original order: 40, 35, 45, 35 and 50. The common array values task needs a clear rule about order, duplicates and how many elements exist.
A set-style intersection reports distinct values present in both sequences; its duplicate and ordering policy should be stated explicitly.
Before this exercise, review array elements and bounds and function parameters. The worked program below applies those ideas to this specific problem.
Explain
Explain why common array values uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Compare with the values 99 and 100 and verify that there are no matches.
Common Array Values: follow the data
Given data
A class stores five marks in their original order: 40, 35, 45, 35 and 50. The common array values task needs a clear rule about order, duplicates and how many elements exist.
Rule
A set-style intersection reports distinct values present in both sequences; its duplicate and ordering policy should be stated explicitly.
Observed result
35 50
Understand the operation
This small scan preserves the first array’s first-occurrence order. The earlier-value check prevents the repeated mark 35 from being printed twice.
The complete example knows its five-element bound. In a reusable function, pass an explicit element count with the array pointer; a pointer alone does not report that bound. This sample has two equal marks, which makes duplicate handling observable.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
int main(void) {
int marks[] = {40, 35, 45, 35, 50};
int count = (int)(sizeof marks / sizeof marks[0]);
int other[] = {35, 42, 50};
for (int i = 0; i < count; i++) {
int seen = 0, present = 0;
for (int j = 0; j < i; j++) if (marks[j] == marks[i]) seen = 1;
for (int j = 0; j < 3; j++) if (other[j] == marks[i]) present = 1;
if (!seen && present) printf("%d ", marks[i]);
}
putchar('\n');
return 0;
}
Expected output
35 50
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int marks[] = {40, 35, 45, 35, 50};
int count = (int)(sizeof marks / sizeof marks[0]);
int other[] = {35, 42, 50};
for (int i = 0; i < count; i++) {
int seen = 0, present = 0;
for (int j = 0; j < i; j++) if (marks[j] == marks[i]) seen = 1;
for (int j = 0; j < 3; j++) if (other[j] == marks[i]) present = 1;
if (!seen && present) printf("%d ", marks[i]);
}
putchar('\n');
return 0;
}
40 has no match, 35 matches but is emitted once, 45 has no match, and 50 matches.
Compare the resulting output with the original sequence. A changed order and a changed set of values are different effects; decide which your operation promises.
Follow the worked calculation
Start with the sample
40 has no match, 35 matches but is emitted once, 45 has no match, and 50 matches.
Apply the rule
Compare the resulting output with the original sequence. A changed order and a changed set of values are different effects; decide which your operation promises.
Check the result
35 50
35 50
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int marks[] = {40, 35, 45, 35, 50};
int count = (int)(sizeof marks / sizeof marks[0]);
int other[] = {35, 42, 50};
for (int i = 0; i < count; i++) {
int seen = 0, present = 0;
for (int j = 0; j < i; j++) if (marks[j] == marks[i]) seen = 1;
for (int j = 0; j < 3; j++) if (other[j] == marks[i]) present = 1;
if (!seen && present) printf("%d ", marks[i]);
}
putchar('\n');
return 0;
}
Compare with the values 99 and 100 and verify that there are no matches.
Worked answer
The complete answer keeps the five valid input elements and applies the stated alternative. Read the output against the original array rather than assume that every loop should sort, remove or overwrite values.
C17 · complete program
#include <stdio.h>
int main(void) {
int marks[] = {40, 35, 45, 35, 50};
int count = (int)(sizeof marks / sizeof marks[0]);
int other[] = {99, 100};
int matches = 0;
for (int i = 0; i < count; i++) for (int j = 0; j < 2; j++) if (marks[i] == other[j]) matches++;
printf("Matches: %d\n", matches);
return 0;
}
Expected output
Matches: 0
Run in C compiler →Recognize the important boundaries
Nested scans are quadratic for similarly sized lists. A sorted two-pointer approach or a suitable set representation can improve large-input behavior.
The sample marks and their sums fit int. General input requires both valid array bounds and an arithmetic-range contract.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
A set-style intersection reports distinct values present in both sequences; its duplicate and ordering policy should be stated explicitly.
Continue with array indexes and bounds.
Continue with pointer and count contracts.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.