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
-
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. -
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. -
Variable Initialization:
int foundDuplicate = 0;
We initialize a variable to keep track of whether any duplicates exist in the array.
-
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 atarr[i]
matches any subsequent element. - If a match is found, we print the duplicate, set
foundDuplicate
to 1, and usebreak
to exit the inner loop to avoid printing duplicates multiple times.
- The outer loop iterates over each element of the array using index
-
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. -
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.