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
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 . . .
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 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.
An array of characters contains a few letters. Write a complete C program that will display...
An array of characters contains a few letters. Write a complete C program that will display the output as shown below. Lets assume the array contains =”abcde”. The program should be able to work with any array size, configurable in the program. Expected output Original array = [ a b c d e] a a b a b c a b c d a b c d e
Write a C++ program to perform the following tasks     a) Declare an integer array of...
Write a C++ program to perform the following tasks     a) Declare an integer array of size 1000.     b) Initialize the array with random values between 1 and 9.     c) Write the code to find and print, how many 1’s occur in the array.
{ "array": [ "52", "26", "39", "15" ] } Pseudocode Program Write the pseudocode for our...
{ "array": [ "52", "26", "39", "15" ] } Pseudocode Program Write the pseudocode for our sort algorithm. Note that your pseudocode must match the algorithm described above. Not just any sorting algorithm will work! Your program must do the following: Prompt the user for a file containing a collection of words. Read the file in JSON format. Perform a sort on the list of words. Display the contents of the list on the screen.