String - Repeating Alphabets
Java:
C++:
Given
a string S as the input, print the distinct alphabets in S that occur
more than once. The alphabets must be printed based on the order of
their occurrence in S.
Input Format:
The first line contains S.
The first line contains S.
Output Format:
The first line contains the distinct alphabets in S that occur more than once.
The first line contains the distinct alphabets in S that occur more than once.
Boundary Conditions:
2 <= LENGTH of S <= 200
2 <= LENGTH of S <= 200
Example Input/Output 1:
Input:
Apple
Input:
Apple
Output:
p
p
Example Input/Output 2:
Input:
environment
Input:
environment
Output:
en
Code:en
Java:
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.next();
int i;
List<Character> li=new
LinkedList<Character>();
Set<Character> st=new
LinkedHashSet<Character>();
for(i=0;i<str.length();i++)
{
li.add(str.charAt(i));
st.add(str.charAt(i));
}
for(Character c: st)
{
if(Collections.frequency(li,c)>1)
System.out.print(c);
}
}
}
C++:
#include<iostream>
#include<string.h>
using namespace std;
int main(int argc, char** argv)
{
char s[200];
cin>>s;
int n=strlen(s);
int i,j,f=0;
for(i=0;i<n;i++)
{
for(j=i+1;j<n&&s[i]!='*';j++)
{
if(s[i]==s[j])
{
s[j]='*';
f=1;
}
}
if(f==1)
{
cout<<s[i];
f=0;
}
}
}
#include<string.h>
using namespace std;
int main(int argc, char** argv)
{
char s[200];
cin>>s;
int n=strlen(s);
int i,j,f=0;
for(i=0;i<n;i++)
{
for(j=i+1;j<n&&s[i]!='*';j++)
{
if(s[i]==s[j])
{
s[j]='*';
f=1;
}
}
if(f==1)
{
cout<<s[i];
f=0;
}
}
}
PYTHON 3
ReplyDeletes=input()
op=''
for i in s:
if(s.count(i)>1):
if(i not in op):
op+=i
print(op)