C Programming – HackerRank Solution | Repeating Element |

Write a Modular C Programming code to solve Repeating Element, Given an array of integers find the first repeating element

Given an array of integers find the first repeating element.

Input Format

First Line contains the ‘N’ number of array elements, followed by ‘N’ array elements

Constraints

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

Output Format

Output the first repeating element

Sample Input 0

7
10 5 3 4 3 5 6

Sample Output 0

5

Explanation 0

Even though 3 is repeating first, 5 comes before 3.

Sample Input 1

-5

Sample Output 1

Invalid

Sample Input 2

5
1 2 3 4 5

Sample Output 2

No repeating element

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(int a[30],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
}
void repaeting(int a[30],int n)
{
    int i,j,copy=0,st=0;
    for(i=0;i<n;i++)
    {
        copy=a[i];
        for(j=i+1;j<n;j++) 
{ 
if(copy==a[j]) 
{ 
printf("%d",copy); 
exit(0); 
} 
else st=1; 
} 
} 
if(st==1) 
{ 
printf("No repeating element\n");
 } 
} 

int main() 
{ int a[30],n; 
scanf("%d",&n); 
if(n>0)
    {
    read(a,n);
    repaeting(a,n);  
    }
    else 
        printf("Invalid\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)

7
10 5 3 4 3 5 6
Your Output (stdout)

5
Expected Output

5

 

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