CategoryC Program

C Program to find elements that exist in the first array but not in the second

Learn how to find distinct elements present in the first C array but absent from the second, with validated code, duplicate handling, and examples.

The elements that exist in the first array but not in the second form the set difference of the two arrays. This operation is written as first - second or A - B.

For example:

First array:  1 2 2 3 5 -1
Second array: 3 2 7
First - second: 1 5 -1

The values 2 and 3 are excluded because they appear in the second array. The result preserves the order of the first array and includes each remaining value only once.

C Program to Find Elements in the First Array but Not the Second

#include <stdio.h>

#define MAX_SIZE 100

int main(void) {
    int first[MAX_SIZE];
    int second[MAX_SIZE];
    int difference[MAX_SIZE];
    int firstSize;
    int secondSize;
    int differenceCount = 0;

    printf("Enter the number of elements in the first array: ");
    if (scanf("%d", &firstSize) != 1 ||
        firstSize < 1 || firstSize > MAX_SIZE) {
        printf("Please enter a size between 1 and %d.\n", MAX_SIZE);
        return 1;
    }

    printf("Enter %d elements for the first array:\n", firstSize);
    for (int index = 0; index < firstSize; index++) {
        if (scanf("%d", &first[index]) != 1) {
            printf("Invalid array element.\n");
            return 1;
        }
    }

    printf("Enter the number of elements in the second array: ");
    if (scanf("%d", &secondSize) != 1 ||
        secondSize < 1 || secondSize > MAX_SIZE) {
        printf("Please enter a size between 1 and %d.\n", MAX_SIZE);
        return 1;
    }

    printf("Enter %d elements for the second array:\n", secondSize);
    for (int index = 0; index < secondSize; index++) {
        if (scanf("%d", &second[index]) != 1) {
            printf("Invalid array element.\n");
            return 1;
        }
    }

    for (int firstIndex = 0; firstIndex < firstSize; firstIndex++) {
        int existsInSecond = 0;
        int alreadyAdded = 0;

        for (int secondIndex = 0;
             secondIndex < secondSize;
             secondIndex++) {
            if (first[firstIndex] == second[secondIndex]) {
                existsInSecond = 1;
                break;
            }
        }

        for (int resultIndex = 0;
             resultIndex < differenceCount;
             resultIndex++) {
            if (first[firstIndex] == difference[resultIndex]) {
                alreadyAdded = 1;
                break;
            }
        }

        if (!existsInSecond && !alreadyAdded) {
            difference[differenceCount] = first[firstIndex];
            differenceCount++;
        }
    }

    printf("Elements in the first array but not the second: ");
    if (differenceCount == 0) {
        printf("None");
    } else {
        for (int index = 0; index < differenceCount; index++) {
            printf("%d ", difference[index]);
        }
    }

    printf("\n");
    return 0;
}

Sample Output

Enter the number of elements in the first array: 6
Enter 6 elements for the first array:
1 2 2 3 5 -1
Enter the number of elements in the second array: 3
Enter 3 elements for the second array:
3 2 7
Elements in the first array but not the second: 1 5 -1

How the Program Works

For every element in the first array, the program:

  1. Searches the second array for the same value.
  2. Searches the result array to see whether that value has already been added.
  3. Adds the value only when it is absent from the second array and absent from the result.

Here is a dry run for the sample arrays:

Candidate from first arrayIn second array?Already added?Action
1NoNoAdd 1
2YesNoSkip
2YesNoSkip
3YesNoSkip
5NoNoAdd 5
-1NoNoAdd -1

The final difference is 1, 5, -1.

Why Check Whether a Value Was Already Added?

Set difference normally contains distinct values. If the first array is 1, 1, 4 and the second array is 2, 3, the result should be 1, 4, not 1, 1, 4.

The alreadyAdded check prevents duplicate copies from entering the result while preserving the order of first appearance.

Why Is Array Difference Directional?

Array difference is not commutative. In general:

A - B is not the same as B - A

For example:

A: 1 2 3
B: 2 3 4
A - B: 1
B - A: 4

This program specifically calculates values in the first array that do not occur in the second.

Set Difference Versus Multiset Difference

The program treats the arrays like sets and prints each result once. A multiset difference subtracts occurrence counts instead.

First array:  2 2 2 5
Second array: 2 7
Distinct set difference: 5
Multiset difference: 2 2 5

The two remaining copies of 2 appear only in the multiset version. This article implements the distinct set difference.

What If Every First-Array Value Exists in the Second?

When every distinct value from the first array also occurs in the second, differenceCount remains 0:

First array:  2 4 2
Second array: 1 2 3 4
Elements in the first array but not the second: None

Does It Work with Negative Numbers and Zero?

Yes. Equality comparison works for every int value:

First array:  -2 0 5 -2
Second array: 0 7
First - second: -2 5

Difference Versus Common Elements

The common-elements operation keeps values present in both arrays. The difference operation does the opposite for the first array:

  • Intersection: values in both the first and second arrays.
  • Difference first - second: values in the first array but absent from the second.

For A = {1, 2, 3} and B = {2, 4}, the intersection is {2} and A - B is {1, 3}.

Alternative Approaches

  • Sort and use two pointers: Can improve performance for large arrays but changes their order unless copies are made.
  • Frequency table: Provides linear time for a small, known value range but requires careful handling of negative and large values.
  • Hash set: Offers expected O(n + m) time for arbitrary values, but standard C does not include a built-in hash-set type.

The nested-loop approach is straightforward, supports all int values, and preserves both input arrays.

Time and Space Complexity

If the first array has n elements, the second has m, and the result contains k values:

  • Time complexity: O(n × m + n × k). For arrays of similar size, the worst case is O(n²).
  • Extra space complexity: O(k) for the distinct result values, where k ≤ n.

Common Mistakes

  • Calculating the intersection instead of the difference.
  • Reversing the operands and computing second - first.
  • Printing duplicate result values when a distinct difference is required.
  • Comparing only values at matching indices.
  • Assuming both arrays have the same size.
  • Confusing set difference with multiset difference.

By checking each first-array value against the second array and preventing duplicate results, the program computes the distinct directional difference while preserving first-array order.