Question
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
EXPLANATION
In this program, you need to print the duplicate elements present in the array.
Give explanation of the functionality of this same program
Answers
GPT-4o mini
Certainly! Below is a C program to print the duplicate elements of an array. Following the program, I'll explain how it works.
```c
#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**:
```c
#include <stdio.h>
```
This line includes standard input/output functions, which allows the program to use the `printf` function for output.
2. **Function Declaration**:
```c
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**:
```c
int foundDuplicate = 0;
```
We initialize a variable to keep track of whether any duplicates exist in the array.
4. **Nested Loop to Find Duplicates**:
```c
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**:
```c
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**:
```c
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.
```c
#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**:
```c
#include <stdio.h>
```
This line includes standard input/output functions, which allows the program to use the `printf` function for output.
2. **Function Declaration**:
```c
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**:
```c
int foundDuplicate = 0;
```
We initialize a variable to keep track of whether any duplicates exist in the array.
4. **Nested Loop to Find Duplicates**:
```c
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**:
```c
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**:
```c
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.