Duplicate Elements: Find duplicates in an array

This program demonstrates how to find duplicate elements in an array using C. The algorithm iterates through the array and uses another array to track seen elements. If an element is found again, it is identified as a duplicate.

C Program

#include <stdio.h>
#include <stdbool.h>

void findDuplicates(int arr[], int size) {
    bool seen[1000] = {false};  // Assuming values are less than 1000
    bool duplicates[1000] = {false};

    for (int i = 0; i < size; i++) {
        if (seen[arr[i]]) {
            duplicates[arr[i]] = true;
        } else {
            seen[arr[i]] = true;
        }
    }

    printf("Duplicate elements: ");
    bool found = false;
    for (int i = 0; i < 1000; i++) {
        if (duplicates[i]) {
            printf("%d ", i);
            found = true;
        }
    }
    if (!found) {
        printf("No duplicates found.");
    }
    printf("\n");
}

int main() {
    int array[] = {1, 2, 3, 4, 5, 6, 7, 2, 3, 8, 9, 10, 10};
    int size = sizeof(array) / sizeof(array[0]);

    findDuplicates(array, size);

    return 0;
}

Explanation

The program includes the following components:

  • #include <stdio.h>: Includes the standard I/O library for input and output functions.
  • #include <stdbool.h>: Includes the standard Boolean library for using true and false values.
  • void findDuplicates(int arr[], int size): Defines a function that takes an array of integers and its size as input and prints the duplicate elements.
  • bool seen[1000] = {false};: Initializes a Boolean array to keep track of elements that have been encountered in the array. The size 1000 is assumed based on the possible range of values in the array.
  • bool duplicates[1000] = {false};: Initializes a Boolean array to store duplicate elements.
  • for (int i = 0; i < size; i++): Iterates through each element in the array.
  • if (seen[arr[i]]): Checks if the element is already in the seen array. If it is, the element is a duplicate.
  • duplicates[arr[i]] = true;: Adds the duplicate element to the duplicates array.
  • seen[arr[i]] = true;: Marks the element as seen.
  • printf("Duplicate elements: ");: Prints the start of the duplicate elements output.
  • for (int i = 0; i < 1000; i++): Iterates through the duplicates array to print the duplicate elements.
  • if (!found): Checks if no duplicates were found and prints an appropriate message.
  • int main(): The main function that initializes an array and calls the findDuplicates function.
  • printf("%d ", i): Prints each duplicate element.

The example array int array[] = {1, 2, 3, 4, 5, 6, 7, 2, 3, 8, 9, 10, 10}; contains the duplicates 2, 3, and 10. When the findDuplicates function is called, it will print:

Duplicate elements: 2 3 10

 

Explanation

  • C Program: The provided C program uses arrays to identify duplicate elements in an array. The findDuplicates function iterates through the input array, using a Boolean array seen to track elements that have been encountered. If an element is already in the seen array, it is added to the duplicates array.
  • Output: The output of the program will list all the duplicate elements found in the array.

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)