简体   繁体   中英

How to remove repeated letter from words in Java

i have problem writing java code to remove repeated letters from word.This code will remove repeated letter by accepting only one of the letter which is repeating . Suppose, if input is "SUSHIL" then output would be "SUHIL". This java code i write.

import java.io.*;
import java.util.*;

public class Repeat
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        char ch1, ch2;
        int i, j;
        int l = name.length();
        String result = "";
        for (i = 0; i < l; i++)
        {
            for (j = 1; j < l; j++)
            {
                ch1 = name.charAt(i);
                ch2 = name.charAt(j);
                if (ch1 != ch2)
                {
                    result = result + ch1;
                    break;
                }
            }
        }
        System.out.println("Output:" + result);
    }
}

try this:

    private static String removeRepeat(String input){

    Set<Character> str = new LinkedHashSet<Character>();

    for(int n=0;n<input.length();n++){

        str.add(input.charAt(n));

    }

    return str.toString();

}

good point from the comment, changed to LinkedHashSet.

It may be the crap code, but what I mean is don't reinvent the wheel, only if you have to

    char ch1,ch2;
    int l=name.length();
    String result="";
    for(int i=0;i<l;i++){ 
        if(name.indexOf(name.charAt(i))==i){
             result+=name.charAt(i);
        }
    }
    System.out.println(result);

input = SUSHSILHI
output = SUHIL

You should do the opposite: add the first letter to result, then check if the next letter is already in result:

boolean exist=false;
result=name.charAt(0);
for (i=1; i<l;i++) {
    exist=false;
    int j=0;
    while (exist=false && j<i) {
        if(name.charAt(i)==charAt(j)) {
             exist=true;
        }
        j++;
    }
    if(exist==false){
         result=result+name.charAt(i);
    }
}

The for checks for all the string name, then the while checks for the characters already in result, if it doesn't already exist, else it doesn't do anything.

Using indexOf() , one for loop should work, like below

 String name="SUSHIL";
    String newName="";
    int i=0;
    int l=name.length();
    for(i=0;i<l;i++)
        {
           char ch1=name.charAt(i);
            if(!(newName.indexOf(ch1)>-1))
                {
                    newName=newName + ch1;
                }
        }
    System.out.println("Output:"+newName);
        String name = "SUSHIL";
        char ch1 = 0, ch2;
        int i, j;
        int l = name.length();
        StringBuilder sb = new StringBuilder();
        for (i = 0; i < l; i++)
        {
            //this is used to append char to StringBuilder
            boolean shouldAppend = true;
            //if we don't check if the length is equal to 0 to start then the below loop will never run and the result would be an empty string so just append the first character to the StringBuilder
            if (sb.length() == 0)
            {
                sb.append(name.charAt(i));
                shouldAppend = false;
            }
            else
            {
                for (j = 0; j < sb.length(); j++)
                {
                    ch1 = name.charAt(i);
                    ch2 = sb.charAt(j);
                    if (ch1 == ch2)
                    {
                        //StringBuilder contains ch1 so turn shouldAppend to false and break out of this inner loop
                        shouldAppend = false;
                        break;
                    }
                }
            }
            if (shouldAppend) sb.append(ch1);

        }
        System.out.println("Output:" + sb.toString());

Try:

//Globally
List<Character> list = new ArrayList<Character>();    

public String remRepeats(String original)
{
   char ch = original.charAt(0);        

    if (original.length() == 1)
        return original;

    if (list.contains(ch))
        return remRepeats(original.substring(1));
    else
    {
        list.add(ch);
        return ch + remRepeats(original.substring(1));
    }
}
List<Character> characters = new ArrayList<>();
char[] chars = name.toCharArray();
StringBuilder stringBuilder = new StringBuilder();
for(char currChar:chars) {
   if (!characters.contains(currChar)) {
         characters.add(currChar);
         stringBuilder.append(currChar);
        }
    }
System.out.println(stringBuilder);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM