Array elements that appear more than once in C?

An array is a container of elements of the same data type. Elements can appear in any order and frequency within an array. In this article, we will learn how to find elements that appear more than once in an array.

Syntax

for (i = 0; i < n; i++) {
    count = 0;
    for (j = 0; j < n; j++) {
        if (arr[i] == arr[j]) {
            count++;
        }
    }
    if (count > 1) {
        // Element appears more than once
    }
}

Algorithm

  1. For each element in the array, count its occurrences
  2. Compare the current element with all other elements
  3. If count is greater than 1, the element is duplicate
  4. Print duplicate elements only once

Method 1: Using Nested Loops

This approach uses two nested loops to count occurrences of each element −

#include <stdio.h>

int main() {
    int arr[] = {5, 11, 11, 2, 1, 4, 2};
    int n = 7;
    int i, j, count;
    
    printf("Elements that appear more than once: ");
    
    for (i = 0; i < n; i++) {
        count = 0;
        
        /* Count occurrences of arr[i] */
        for (j = 0; j < n; j++) {
            if (arr[i] == arr[j]) {
                count++;
            }
        }
        
        /* Check if element appears more than once and hasn't been printed */
        if (count > 1) {
            /* Check if this is the first occurrence */
            int isFirstOccurrence = 1;
            for (int k = 0; k < i; k++) {
                if (arr[k] == arr[i]) {
                    isFirstOccurrence = 0;
                    break;
                }
            }
            
            if (isFirstOccurrence) {
                printf("%d ", arr[i]);
            }
        }
    }
    
    printf("<br>");
    return 0;
}
Elements that appear more than once: 11 2 

Method 2: Using Frequency Array

This method uses an auxiliary array to store frequency counts −

#include <stdio.h>

int main() {
    int arr[] = {3, 1, 3, 4, 1, 5, 4};
    int n = 7;
    int freq[100] = {0}; /* Assuming array elements are < 100 */
    
    /* Count frequency of each element */
    for (int i = 0; i < n; i++) {
        freq[arr[i]]++;
    }
    
    printf("Elements that appear more than once: ");
    
    /* Print elements with frequency > 1 */
    for (int i = 0; i < 100; i++) {
        if (freq[i] > 1) {
            printf("%d ", i);
        }
    }
    
    printf("<br>");
    return 0;
}
Elements that appear more than once: 1 3 4 

Comparison

Method Time Complexity Space Complexity Best For
Nested Loops O(n²) O(1) Small arrays, no extra space
Frequency Array O(n) O(k) Known range of elements

Key Points

  • Method 1 has no space overhead but higher time complexity
  • Method 2 is faster but requires additional memory
  • Always avoid printing duplicate elements multiple times

Conclusion

Finding duplicate elements can be achieved using nested loops for simplicity or frequency arrays for efficiency. Choose the method based on your time and space requirements.

Updated on: 2026-03-15T12:11:04+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements