C Examples

WAP in C that will read an array of integers. after reading array, the program should check if there any duplicate value in the array

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.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

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

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X