C Programming – HackerRank Solution | Duplicates 1 |

Write a Modular C Programming code to solve Duplicates 1, Given an array of integers find the duplicates in it

Given an array of integers find the duplicates in it.

Input Format

First line contains N, number of array elements. Next line contains N array elements separated by a space.

Constraints

N<=10^4 0<=a[i]<=10^9

Output Format

Display Dupliactes separated by a space

Sample Input 0

5
10 20 10 20 10

Sample Output 0

10 20

Sample Input 1

-5

Sample Output 1

Invalid

Sample Input 2

5
10 20 30 40 50

Sample Output 2

No Duplicate

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings

 

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

void read_a(int a[30],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
}

void display_a(int a[30],int n)
{
    int i,j,count=0;
    int b[30];
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(a[i]==a[j])
            {
                b[i]=a[i];
                a[j]=0;
                count++;
            }
        }
        
    }
    if(count==0)
        {
            printf("No Duplicate");
            exit(0);
        }
    for(i=0;i<n;i++)
    {
       if(b[i]!=0)
       {
           printf("%d ",b[i]);
       }
        
    }
}

int main() {
    int n,a[30];
    scanf("%d",&n);
    if(n<1||n>10000)
    {
        printf("Invalid");
        exit(0);
    }
    read_a(a,n);
    display_a(a,n);
    return 0;
}

OUTPUT

Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

5
10 20 10 20 10
Your Output (stdout)

10 20 
Expected Output

10 20

 

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University







Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!