Write a C Program to print the duplicate elements of an array.

EXPLANATION
In this program, you need to print the duplicate elements present in the array.
Give explanation of the functionality of this same program

1 answer

Certainly! Below is a C program to print the duplicate elements of an array. Following the program, I'll explain how it works.

#include <stdio.h>

void printDuplicates(int arr[], int size) {
    int i, j;
    int foundDuplicate = 0;  // To check if we found any duplicates

    // Iterate through the array
    for (i = 0; i < size; i++) {
        // Check for duplicates of arr[i]
        for (j = i + 1; j < size; j++) {
            if (arr[i] == arr[j]) {
                printf("Duplicate element found: %d\n", arr[i]);
                foundDuplicate = 1;  // We found at least one duplicate
                break;  // Break the inner loop to avoid printing the same duplicate multiple times
            }
        }
    }
    
    if (!foundDuplicate) {
        printf("No duplicate elements found.\n");
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5, 1, 2, 6, 7, 3};  // Example array
    int size = sizeof(arr) / sizeof(arr[0]);  // Determine the size of the array

    printDuplicates(arr, size);  // Call the function to print duplicates

    return 0;
}

Explanation of the Program

  1. Include the Necessary Header:

    #include <stdio.h>
    

    This line includes standard input/output functions, which allows the program to use the printf function for output.

  2. Function Declaration:

    void printDuplicates(int arr[], int size) {
    

    Here we declare a function printDuplicates that takes an array of integers (arr[]) and its size (size) as parameters. This function is responsible for finding and printing the duplicate elements.

  3. Variable Initialization:

    int foundDuplicate = 0; 
    

    We initialize a variable to keep track of whether any duplicates exist in the array.

  4. Nested Loop to Find Duplicates:

    for (i = 0; i < size; i++) {
        for (j = i + 1; j < size; j++) {
            if (arr[i] == arr[j]) {
                printf("Duplicate element found: %d\n", arr[i]);
                foundDuplicate = 1;  
                break; 
            }
        }
    }
    
    • The outer loop iterates over each element of the array using index i.
    • The inner loop runs from i + 1 to the end of the array, checking if the element at arr[i] matches any subsequent element.
    • If a match is found, we print the duplicate, set foundDuplicate to 1, and use break to exit the inner loop to avoid printing duplicates multiple times.
  5. Check for Duplicates:

    if (!foundDuplicate) {
        printf("No duplicate elements found.\n");
    }
    

    After checking all elements, we check if foundDuplicate was ever set to 1. If not, we inform the user that no duplicates were found.

  6. Main Function:

    int main() {
        int arr[] = {1, 2, 3, 4, 5, 1, 2, 6, 7, 3}; 
        int size = sizeof(arr) / sizeof(arr[0]); 
    
        printDuplicates(arr, size); 
    
        return 0;
    }
    
    • We define an example array with some duplicate values.
    • We calculate the size of the array using sizeof(arr) / sizeof(arr[0]).
    • We call the printDuplicates function to check for and print duplicate elements.
    • Finally, we return 0, indicating successful execution of the program.

Example Output:

For the provided example array {1, 2, 3, 4, 5, 1, 2, 6, 7, 3}, the output will be:

Duplicate element found: 1
Duplicate element found: 2
Duplicate element found: 3

This program effectively highlights how to find and showcase duplicates in a given array.

Similar Questions
  1. Write a C Program to print the duplicate elements of an array.In this program, you need to print the duplicate elements present
    1. answers icon 1 answer
  2. Q1. Write a C Program to print the duplicate elements of an array.In this program, you need to print the duplicate elements
    1. answers icon 1 answer
  3. 1. Elements – sequence-series outputtingWrite a program that prints the following elements: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19,
    1. answers icon 0 answers
  4. Elements – sequence-series outputtingWrite a program that prints the following elements: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19,
    1. answers icon 2 answers
more similar questions