Basic

Switch Case

Goto

Operators

if Statement

Nested if

While Loop

For Loop

Array

Patterns

Excersises


Find duplicate values in a given array in c program


Write a program to print the duplicate values in a given array in c program.

Example : pgm.c
#include<stdio.h>
int main(){
    int n=10,k=0,i,j,h,ch;
    int a[10]={5,1,5,6,7,8,5,4,1,4};
    int dup[100];
    for(i=0;i<n;i++){
        for(j=i+1;j<n;j++){
            if(a[i]==a[j]){
                ch=0;
                for(h=0;h<k;h++){
                    if(dup[h]==j){
                        ch=1;
                        break;
                    }
                }
                if(ch==0){
                    dup[k]=j;
                    k++;
                }
            }
        }
    }
    printf("\nDuplicate Values are ");
    for(i=0;i<k;i++){
        printf("\na[%d] = %d",dup[i],a[dup[i]]);
    }
    return 0;
}

Output:

Duplicate Values are
a[2] = 5
a[6] = 5
a[8] = 1
a[9] = 4