Remove all duplicates from a given string
PROBLEM :
Given a string, the task is to remove all duplicate characters from the string and print the resultant string. The order of remaining characters in the output should be same as in the original string.
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
Each test case contains a string of length N.
Output:
Print the resultant substring after removing duplicate characters from string.
Constraints:
1 = T = 100
1 = N = 100
Example:
Input:
2
geeksforgeeks
HappyNewYear
Output:
geksfor
HapyNewYr
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<string.h>
#include<malloc.h>
char* Remove_duplicates(char*) ;
int main()
{
int t ;
char *str;
str=(char*)malloc(100*sizeof(char)) ;
cin>>t ;
while(t--)
{
cin>>str ;
str=Remove_duplicates(str) ;
cout<<str<<endl ;
}
return 0;
}
char* Remove_duplicates(char *str)
{
int l,i,j ;
l=strlen(str) ;
char *strn ;
strn=(char*)malloc(100*sizeof(char)) ;
int hash[256]={0} ;
for(i=0;i<l;i++)
hash[str[i]]++ ;
j=0 ;
for(i=0;i<l;i++)
{
if(hash[str[i]]!=0)
{
strn[j]=str[i] ;
j++ ;
}
hash[str[i]]=0 ;
}
strn[j]='\0' ;
return strn ;
}
---------------------------------------------------------------------------------
Given a string, the task is to remove all duplicate characters from the string and print the resultant string. The order of remaining characters in the output should be same as in the original string.
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
Each test case contains a string of length N.
Output:
Print the resultant substring after removing duplicate characters from string.
Constraints:
1 = T = 100
1 = N = 100
Example:
Input:
2
geeksforgeeks
HappyNewYear
Output:
geksfor
HapyNewYr
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<string.h>
#include<malloc.h>
char* Remove_duplicates(char*) ;
int main()
{
int t ;
char *str;
str=(char*)malloc(100*sizeof(char)) ;
cin>>t ;
while(t--)
{
cin>>str ;
str=Remove_duplicates(str) ;
cout<<str<<endl ;
}
return 0;
}
char* Remove_duplicates(char *str)
{
int l,i,j ;
l=strlen(str) ;
char *strn ;
strn=(char*)malloc(100*sizeof(char)) ;
int hash[256]={0} ;
for(i=0;i<l;i++)
hash[str[i]]++ ;
j=0 ;
for(i=0;i<l;i++)
{
if(hash[str[i]]!=0)
{
strn[j]=str[i] ;
j++ ;
}
hash[str[i]]=0 ;
}
strn[j]='\0' ;
return strn ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment