problem no 43:(array problem):Remove duplicate elements from sorted Array .

QUESTION:

Given a sorted array A of size N, delete all the duplicates elements from A.


Example 1:

Input:
N = 5
Array = {2, 2, 2, 2, 2}
Output: 2
Explanation: After removing all the duplicates 
only one instance of 2 will remain.


Example 2:

Input:
N = 3
Array = {1, 2, 2}
Output: 1 2 
 
ANSWER:
int remove_duplicate(int a[],int n){
set<int> s1;
for(int i=0;i<n;i++)
{
s1.insert(a[i]);
}

set<int, greater<int> >::iterator itr;
for (itr = s1.begin(); itr != s1.end(); itr++)
{
cout << *itr<<" ";
}
return 0;
}

 

Comments

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

Problem no 16: count the number of squares below N.