forked from Pratiyush27/Arrays_Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_First_Duplicate.c
More file actions
38 lines (36 loc) · 855 Bytes
/
Copy path2_First_Duplicate.c
File metadata and controls
38 lines (36 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// An algorithm for finding the first element in the array
// which is repeated
#include <stdio.h>
#include <stdlib.h>
// Function used to find first Duplicate element
void First_Duplicate(int A[], int size)
{
int i, j;
printf("The First repeating elements is: \n");
// Traversing the array to find the First Duplicate number
for (i = 0; i < size-1; i++)
{
for(j =i+1; j <size; j++)
{
if(A[i] == A[j])
{
printf(" %d ",A[i]);
return; // return the loop when we get first Duplicate Element
}
}
}
}
int main()
{
int i,n;
printf("Enter the number of Elements to be Entered\n");
scanf("%d", &n);
int *A = (int*)malloc(n*sizeof(int));
printf("Enter the Elements of Array\n");
for(i=0;i<n;i++)
{
scanf("%d", &A[i]);
}
First_Duplicate(A, n); // Calling of Function
return 0;
}