简体   繁体   English

如何从Java中的单词中删除重复的字母

[英]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 . 我有编写java代码以删除word中重复的字母的问题。此代码将通过仅接受重复的一个字母来删除重复的字母。 Suppose, if input is "SUSHIL" then output would be "SUHIL". 假设,如果输入为“SUSHIL”,则输出为“SUHIL”。 This java code i write. 我写这个java代码。

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. 从评论的好点,改为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 输入= SUSHSILHI
output = SUHIL 输出= 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. for检查所有字符串名称,然后while检查结果中已存在的字符,如果它尚不存在,则不执行任何操作。

Using indexOf() , one for loop should work, like below 使用indexOf() ,一个for循环应该可以工作,如下所示

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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