C Program to print duplicate elements in an array

Category: C Program

Learn how to write a C program to print duplicate elements in an array. Explore methods using nested loops and frequency arrays, with complete code examples and explanations.

In C programming, managing arrays and processing their elements is a fundamental task. One common requirement is to identify and print duplicate elements in an array. This article will guide you through writing a C program that prints the duplicate elements in an array, along with explanations and sample code.

Steps to Print Duplicate Elements

To print the duplicate elements in an array, we can follow these steps:

  1. Input the Array: Read the array elements from the user.
  2. Identify Duplicate Elements: Check each element to determine if it is a duplicate.
  3. Print Duplicates: Output the duplicate elements to the console.

We'll discuss two methods to achieve this:

  1. Using Nested Loops: This method involves comparing each element with every other element in the array.
  2. Using a Frequency Array: This method uses an auxiliary array to keep track of the frequency of each element.

Method 1: Using Nested Loops

#include <stdio.h>

int main() {
    int size;
    printf("Enter the size of the array: ");
    scanf("%d", &size);

    int arr[size];
    printf("Enter %d elements -\n", size);
    for (int i = 0; i < size; i++) {
        scanf("%d", &arr[i]);
    }

    printf("Duplicate elements in the array are: ");
    for (int i = 0; i < size; i++) {
        for (int j = i + 1; j < size; j++) {
            if (arr[i] == arr[j]) {
                printf("%d ", arr[i]);
                break; // Avoid counting the same duplicate more than once
            }
        }
    }

    return 0;
}

Explanation:

  1. Input the Array: The user inputs the size of the array and the elements.
  2. Identify Duplicate Elements: The program uses two nested loops. The outer loop iterates through each element, and the inner loop checks if the current element is equal to any subsequent element.
  3. Print Duplicates: When a duplicate is found, it is printed and the inner loop breaks to avoid counting the same duplicate more than once.

Advantages:

  • Simple and easy to understand.
  • No additional memory required apart from the input array.

Disadvantages:

  • Time complexity is O(n^2), making it inefficient for large arrays.

Method 2: Using a Frequency Array

#include <stdio.h>

int main() {
    int size;
    printf("Enter the size of the array: ");
    scanf("%d", &size);

    int arr[size];
    printf("Enter %d elements -\n", size);
    for (int i = 0; i < size; i++) {
        scanf("%d", &arr[i]);
    }

    int maxVal = arr[0];
    for (int i = 1; i < size; i++) {
        if (arr[i] > maxVal) {
            maxVal = arr[i];
        }
    }

    int freq[maxVal + 1];
    for (int i = 0; i <= maxVal; i++) {
        freq[i] = 0;
    }

    for (int i = 0; i < size; i++) {
        freq[arr[i]]++;
    }

    printf("Duplicate elements in the array are: ");
    for (int i = 0; i <= maxVal; i++) {
        if (freq[i] > 1) {
            printf("%d ", i);
        }
    }

    return 0;
}

Explanation:

  1. Input the Array: The user inputs the size of the array and the elements.
  2. Find Maximum Value: The program finds the maximum value in the array to determine the size of the frequency array.
  3. Frequency Array: The program initializes a frequency array to zero. It then iterates through the input array, incrementing the frequency of each element.
  4. Print Duplicates: The program iterates through the frequency array, and for each element with a frequency greater than one, it prints the element.

Advantages:

  • More efficient with a time complexity of O(n).
  • Suitable for larger arrays compared to the nested loop method.

Disadvantages:

  • Requires additional memory for the frequency array.
  • Assumes a known range of possible values in the array.

Output

Enter the size of the array: 10
Enter 10 elements -
1
2
1
2
3
4
5
5
6
7
Duplicate elements in the array are: 1 2 5

Recommended Posts