Write a recursive C++ program that find the duplicate elements in an array. Your program shouldn't use any sorting or use any additional array.
#include <iostream.h>
void duplicate(int arr[],int s,int i,int j)
{
if(arr[i]==arr[j] and j-i>1)
{
i=0;
j=j-1;
duplicate(arr,s,i,j);
}
else if(arr[i]==arr[j] and i==j)
{
arr[i]=0;
arr[j]=0;
}
else if(arr[i]!=arr[j] and i<=j-2)
{
i++;
duplicate(arr,s,i,j);
}
else
{arr[j]=0;
i=0;
j=j-1;
duplicate(arr,s,i,j);
}
}
int main() {
int arr[8]={1,2,1,3,1,3,2,1};
int i=0;
int s=sizeof(arr)/sizeof(arr[0]);
int j=s-1;
duplicate(arr,s,i,j);
for ( int i=0; i<s; i++)
{int f=0;
for (int j=0; j<i; j++)
{//cout<<"i="<<i<<"
"<<"j="<<j<<endl;
if (arr[i] == arr[j])
{f=1;
break;
}
}
if(f==0 and arr[i]!=0)
{
cout<<arr[i]<<endl;
}
}
getch();
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.