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.

  1. Explain

    Explain why common array values uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Compare with the values 99 and 100 and verify that there are no matches.

Common Array Values: follow the data

  1. 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.

  2. Rule

    A set-style intersection reports distinct values present in both sequences; its duplicate and ordering policy should be stated explicitly.

  3. Observed result

    35 50

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

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

  1. Start with the sample

    40 has no match, 35 matches but is emitted once, 45 has no match, and 50 matches.

  2. 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.

  3. Check the result

    35 50

    35 50
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

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.

Why is count passed separately to a reusable array function?
Choose one answer. You can try again.

Answer and explanation

The element pointer does not carry the caller’s element count.

Correct. The separate bound defines which accesses are permitted.

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.

    Next lesson: C Program to Print a 2D Array →