0

Find the duplicate element

Assuming an array of 100 elements with values between 1-99,one of the elements is duplicate.write a program to find the duplicate element

23rd Mar 2017, 5:02 AM
Pratul Tripathi
Pratul Tripathi - avatar
5 Respostas
+ 1
#include <stdio.h> void main() { int i,n=0; int arr[100]; int j = 0; int prev = 0; printf("Enter total no. of the elements : "); scanf("%d",&n); printf("Enter total %d elements : \n",n); for(i=0; i<n; i++) scanf("%d",&arr[i]); for(i=0; i<n-1; i++) { j++; if( j < n){ i = prev; } else{ j = i + 1; } if(arr[i] < arr[j]) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } prev = i; } printf("\nSorted Array : "); for (i = 0; i < n; i++) { printf(" %d,",arr[i]); } printf("\nDuplicate :"); for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n ; j++) { if (arr[i] == arr[j]) { printf(" %d,",arr[i]); } } } }
23rd Mar 2017, 5:18 AM
Kaushik Pankhaniya
Kaushik Pankhaniya - avatar
0
thanks bro
23rd Mar 2017, 5:19 AM
Pratul Tripathi
Pratul Tripathi - avatar
0
Is it Useful to you..??
23rd Mar 2017, 5:48 AM
Kaushik Pankhaniya
Kaushik Pankhaniya - avatar
0
it's not showing the. duplicate value.. and i have no need of shorted array..
23rd Mar 2017, 5:50 AM
Pratul Tripathi
Pratul Tripathi - avatar
0
now its work fine... thanks
23rd Mar 2017, 6:25 AM
Pratul Tripathi
Pratul Tripathi - avatar