CategoryC Program

C Program to find the element that occurs the least times in an array

Learn how to find the least frequent element in a C array, with defined tie handling, validated code, examples, a dry run, and complexity analysis.

The element that occurs the least times is the value with the lowest frequency among the values present in an array. We can find it by counting each element's occurrences and keeping the value with the smallest count.

For example:

Array: 4 2 4 3 2 4 8 2

The frequencies are:

4 occurs 3 times
2 occurs 3 times
3 occurs 1 time
8 occurs 1 time

Both 3 and 8 have the minimum frequency. This program returns 3 because it appears earlier in the original array.

C Program to Find the Least Frequent Array Element

#include <stdio.h>

#define MAX_SIZE 100

int main(void) {
    int array[MAX_SIZE];
    int size;

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

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

    int leastFrequent = array[0];
    int lowestFrequency = size + 1;

    for (int current = 0; current < size; current++) {
        int frequency = 0;

        for (int index = 0; index < size; index++) {
            if (array[current] == array[index]) {
                frequency++;
            }
        }

        if (frequency < lowestFrequency) {
            lowestFrequency = frequency;
            leastFrequent = array[current];
        }
    }

    printf("Least frequent element = %d\n", leastFrequent);
    printf("Number of occurrences = %d\n", lowestFrequency);

    return 0;
}

Sample Output

Enter the number of elements: 8
Enter 8 elements:
4 2 4 3 2 4 8 2
Least frequent element = 3
Number of occurrences = 1

How the Program Works

  1. leastFrequent is initialized with the first array element.
  2. lowestFrequency starts at size + 1, which is greater than any possible frequency in the array.
  3. The outer loop selects one candidate value at a time.
  4. The inner loop compares that value with every element and calculates its frequency.
  5. If the frequency is smaller than lowestFrequency, the program updates both the minimum count and its value.
  6. Equal frequencies do not replace the current answer, which preserves the earliest-occurring value.

Here is a compact dry run:

Selected valueFrequencyLowest frequencyLeast frequent value
4334
2334
4334
3113
2313
4313
8113
2313

The value 8 ties with 3, but it does not replace 3 because the update condition uses < rather than <=.

Why Initialize the Frequency to size + 1?

Any value present in an array of size elements must occur between 1 and size times. Therefore, size + 1 is guaranteed to be greater than the first calculated frequency.

This ensures that the first candidate always initializes the result correctly without using an arbitrary magic number.

What Happens When Frequencies Are Tied?

The program returns the tied value that appears earliest in the original array. For 5, 2, 5, 2, 7, 9, both 7 and 9 occur once, so the answer is 7.

The tie behavior comes from this strict comparison:

if (frequency < lowestFrequency)

Using <= would allow a later element with the same frequency to replace the earlier result.

What If Every Element Appears Once?

If every value is unique, every frequency equals 1. The program returns the first element because all values are tied for the minimum frequency.

Array: 6 3 10 -2
Least frequent element: 6
Number of occurrences: 1

What If Every Element Has the Same Value?

When all elements are equal, there is only one distinct value, and its frequency is the array size:

Array: 4 4 4 4
Least frequent element: 4
Number of occurrences: 4

It is both the least frequent and most frequent value because it is the only value present.

Does It Work with Negative Numbers and Zero?

Yes. The algorithm relies only on equality comparisons:

Array: -3 0 -3 5 0 -3
Least frequent element: 5
Number of occurrences: 1

Least Frequent Versus Non-Repeating Elements

A non-repeating element has a frequency of exactly 1. The least frequent element has the minimum frequency present, which may be greater than 1.

For example, in 2, 2, 3, 3, 3, no element is non-repeating, but 2 is still the least frequent value because it occurs twice while 3 occurs three times.

This program always returns a result for a non-empty array.

Alternative Approaches

  • Frequency array: Provides O(n) time for values within a small, known range but needs careful handling of negatives and range limits.
  • Sorting: Groups equal values so runs can be counted in O(n log n) total time, but it changes the original order and complicates the earliest-value tie rule.
  • Hash table: Offers expected O(n) time for arbitrary integers, but standard C has no built-in hash-table type.

The nested-loop solution supports every int, uses constant extra space, and preserves the original order.

Time and Space Complexity

  • Time complexity: O(n²) because every candidate is compared with all n elements.
  • Extra space complexity: O(1) because only counters and result variables are used beyond the input array.

Common Mistakes

  • Initializing lowestFrequency to 0, which no present element can beat.
  • Forgetting to reset frequency for each candidate.
  • Using <= without realizing that it favors later values in a tie.
  • Assuming the least frequent element must occur exactly once.
  • Using array values as frequency-array indices without validating their range.
  • Failing to define how ties should be resolved.

By counting each candidate and retaining the lowest count, the program finds the least frequent element while preserving the array and applying a predictable tie rule.