The Program in C that will read an array of integers. after reading array, the program should check if there any duplicate value in the array is given below:
#include <stdio.h>
int main() {
int n, i, j;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int a[n];
printf("Enter the elements of the array: ");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (a[i] == a[j]) {
printf("Duplicate value found: %d\n", a[i]);
return 0;
}
}
}
printf("No duplicate values found.\n");
return 0;
}
Output:
Enter the number of elements in the array: 4
Enter the elements of the array: 12
11
12
8
Duplicate value found: 12
Pro-Tips💡
This program first takes the number of elements in the array as input, and then declares an array of that size, a
.
Then it reads the elements of the array using a for loop.
After that, it uses two nested for loops to compare each element with every other element in the array.
If a duplicate value is found, the program outputs the duplicate value and exits, otherwise it continues with the next iteration of the outer loop.
If the loop completes successfully, it means that there are no duplicate values, it then prints “No duplicate values found.”
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.