String – Repeating Alphabets

                    String - Repeating Alphabets


 

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.
Output Format:
The first line contains the distinct alphabets in S that occur more than once.
Boundary Conditions:
2 <= LENGTH of S <= 200

Example Input/Output 1:

Input:
Apple
Output:
p
Example Input/Output 2:
Input:
environment
Output:
en

Code:

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;
}
}
}

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *