-3

I am a java noob and I have particular problem. Let us consider a String which multipe repetition of alphabets in it :

String a="AAABBBAACCBBDD"

I have tried this multiple times with for and charAt. Expected output:

"ABCD"

Is there any builtin function for this operation and any ideas to get output ?

3 Answers3

4

You can iterate over the characters of the string to build up a set of the unique characters:

String a = "AAABBBAACCBBDD";
Set<Character> charSet = new HashSet<String>();
for (char c : a.toCharArray())
{
    charSet.add(c);
}

Then you can convert the set to a list and sort it for display/toString() purposes:

List<Character> uniqueCharList = new ArrayList<Character>(charSet);
Collections.sort(uniqueCharList);

// Convert List<Character> to char[]
// see http://stackoverflow.com/q/6649100/139010 for a more concise library call
char[] uniqueCharArray = new char[uniqueCharList.size()];
for (int i=0; i<uniqueCharArray.length; i++)
{
    uniqueCharArray[i] = uniqueCharList.get(i);
}

String result = new String(uniqueCharArray);
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
2

I would do something like

public static String dedup(String text) {
    Set<Character> seen = new LinkedHashSet<Character>();
    StringBuilder sb = new StringBuilder();
    for (char c : text.toCharArray())
        if (seen.add(c))
            sb.append(c);
    return sb.toString();
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • +1; but if the alphabet was `CBAAABBBAACCBBDD`, the linked set will fail. Depending on the requirements it might be necessary to sort the final set... – home Sep 05 '12 at 14:28
  • @home How will the linked set fail? This will print `CBAD` in order of appearance. – Peter Lawrey Sep 05 '12 at 14:30
  • that's just what I meant by 'requirements' - the output might have to be provided in lexicographical/natural order (`ABCD` instead of `CBAD`). – home Sep 05 '12 at 14:36
  • @all: what do blue color text mean ? – tam_ubuuser Sep 05 '12 at 14:37
  • @home I doesn't see how than means the linked set will fail. – Peter Lawrey Sep 05 '12 at 14:37
  • @tam_ubuuser In the code it means classes in Java (i.e. words which start with an upper case letter) Everywhere else its a web page hyper link you can click on. ;) – Peter Lawrey Sep 05 '12 at 14:38
  • 1
    @Peter: ok, 'fail' might be the wrong term (of course it won't fail, apologies), I just meant that it might not be necessary to prefer a LinkedHashSet (again depends on requirements). – home Sep 05 '12 at 14:39
-2

You dont need list or set to this. Just String to store everything. I hope this have not bugs in it.

String result = "";
for(int i = 0; i < a.length(); i++) 
    if(!result.contains(a[i])) {
           result += a[i];          
    }
grachol
  • 82
  • 1
  • 7
  • 3
    -1. This will have _terrible_ asymptotic runtime performance. Each call to `String#contains()` is _O(n)_, and string concatenation in a loop is _O(n²)_. – Matt Ball Sep 05 '12 at 14:29
  • 1
    It works in theory, but what if the string was extremely long? Reconsider your approach. – Franklin Sep 05 '12 at 14:31