explain working of this code.. explain its each line #include using namespace std; // Function to change all duplicate elements to -1 void eliminate_duplicates(int arr[] , int N) { int count=0, i, j; for(i=0; i>n; int a[n]; cout<<"Enter elements in array : "; // Taking input for array elements for(i=0; i>a[i]; } eliminate_duplicates(a,n);
explain working of this code.. explain its each line
#include <bits/stdc++.h>
using namespace std;
// Function to change all duplicate elements to -1
void eliminate_duplicates(int arr[] , int N)
{
int count=0, i, j;
for(i=0; i<N; i++)
{
if(arr[i]!=-1)
{
for(j=i+1; j<N; j++)
{
if(arr[i]==arr[j])
{
count++;
arr[j]=-1;
}
}
}
}
// Printing results
cout<<endl<<"Final state of array is : ";
for(int k=0; k<N; k++)
{
cout<<arr[k]<<" ";
}
N = N-count;
cout<<endl<<"Final value of N is : "<<N;
}
// Driver Function
int main()
{
int i,n ;
cout<<"Enter array size: "; // Taking input for array size
cin>>n;
int a[n];
cout<<"Enter elements in array : "; // Taking input for array elements
for(i=0; i<n; i++)
{
cin>>a[i];
}
eliminate_duplicates(a,n);
return 0;
}
Step by step
Solved in 2 steps