2015-06-12 77 views
1

我有寫java代碼以從word中刪除重複的字母的問題。此代碼將通過接受只重複其中一個字母來刪除重複的字母。假設,如果輸入是「SUSHIL」,那麼輸出將是「SUHIL」。 這是我編寫的Java代碼。如何從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); 
    } 
} 

回答

2

試試這個:

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

} 

從評論好點,改爲LinkedHashSet。

這可能是垃圾代碼,但我的意思是不推倒重來,只有當你有

+0

Java中的'HashSet'不保證保留插入順序。看[這個問題](http://stackoverflow.com/questions/9345651/ordering-of-elements-in-java-hashset)。 – azurefrog

+0

爲什麼要在字符串中添加字符時使用一組字符串?使用一組字符! – Curious

+0

+ Jegg感謝它真的有幫助 – Manish

0

你應該做相反:添加的第一個字母導致,則檢查下一個信已經在結果:

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

的用於檢查所有的字符串名稱,然後爲字符,而檢查已經結果,如果它不存在,否則它不會做任何事情。

+0

這不會編譯。除此之外,'charAt()'方法返回一個'char',所以你不能像你所做的那樣把結果賦值給'String'類型的變量。 – azurefrog

+0

感謝您的幫助@Corbac。它給我錯誤在第二行字符不能被轉換爲字符串。 – Manish

0

使用indexOf(),一個用於環路應該工作,如下面

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

輸入= SUSHSILHI
輸出= SUHIL

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

嘗試:

//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)); 
    } 
} 
0
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); 
+1

如果你描述你的解決方案併發布你的代碼,你的答案會更好。 –

+0

謝謝尼爾,我會牢記這一點。 – navinmurrari

相關問題