Question

Write a recursive C++ program that find the duplicate elements in an array. Your program shouldn't...

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.

Homework Answers

Answer #1

#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;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In java 1. Write a recursive algorithm to add all the elements of an array of...
In java 1. Write a recursive algorithm to add all the elements of an array of n elements 2. Write a recursive algorithm to get the minimum element of an array of n elements 3. Write a recursive algorithm to add the corresponding elements of two arrays (A and B) of n elements. Store the results in a third array C. 4. Write a recursive algorithm to get the maximum element of a binary tree 5. Write a recursive algorithm...
1. Write a C++ program that implements the recursive function isMember as declared above. Pass an...
1. Write a C++ program that implements the recursive function isMember as declared above. Pass an array to the function by reference. Here are two sample runs of the program. The elements of the array are: 0 4 5 6 7 Enter the element you want to find: 6 Element is in the array Press any key to continue . . .
1) a. Write the C++ program that uses a recursive function to print the elements of...
1) a. Write the C++ program that uses a recursive function to print the elements of a list in reverse order. b. Write the pseudocode for the program.
Write a recursive array that gives the min of an array C
Write a recursive array that gives the min of an array C
Write a C program to print the elements of an array in reverse order using pointer.
Write a C program to print the elements of an array in reverse order using pointer.
Write a program to determine the minimum element in an array of ten elements. The program...
Write a program to determine the minimum element in an array of ten elements. The program should have the following: 1. Class Name as ArrayProcessing. The main method should create an array of size 10 2. There should be two methods besides the main method in the class namely inputArray and MinimumElement 3. InputArray method should assign the ten elements in the array. Use scanner to input these elements. The array of 10 numbers in the method "InputArray" should be...
C program question: Write a small C program connect.c that: 1. Initializes an array id of...
C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and changing all the entries with the same name as p to...
In this program, you should define an array of 10 elements in your data segment with...
In this program, you should define an array of 10 elements in your data segment with these values: ? = {11, 12,−10, 13, 9, 12, 14, 15,−20, 0} a. Write a function which finds the maximum value of this array. b. Write another function which calculates the summation of this array. c. Call these functions in your main program, and print the outputs of these functions to the user i. “The maximum is 15” ii. “The summation is 56” d....
Write a program in c++ to Convert an array of inches to an array of centimeters....
Write a program in c++ to Convert an array of inches to an array of centimeters. The program should contain a function called inchesTOcm with three parameters (inches array that contains the values in inches, cm array to save the result in, and an integer variable that defines the length of the array). In the main function: 1. Define an array (inches) of length 3. 2. Initialize the array by asking the user to input the values of its elements....
C++ Write a recursive routine that will have a character array and an index as parameters...
C++ Write a recursive routine that will have a character array and an index as parameters and will return the count of all vowels (assume lowercase). You may assume that the index starts out at the END of the array.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT