In: Computer Science
C++ Programming using iostream and namespace std
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)
Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
/*If you any query do comment in the comment section else like the solution*/
#include
using namespace std;
void findDuplicate(int arr[], int n)
{
for(int i=0;i
for(int j=0;j
if(i!=j && arr[i]==arr[j]){
cout<
}
}
}
cout<<-1;
}
int main()
{
int n;
cout<<"Enter size of array : ";
cin>>n;
int arr[n];
cout<<"Enter array elements : ";
for(int i=0;i
findDuplicate(arr,n);
}