delete repeated letters in a word

Discussion in 'Java' started by spidey12345, Feb 8, 2007.

  1. spidey12345

    spidey12345 Guest

    so If i enter the letter blbabh

    the word will acutally become: blah



    // repeatble letters in the key

    char checkkey[] = pkey.toCharArray();

    int pkeysize = checkkey.length;
    int counter = 0;
    for(int i = 1; i< pkeysize; i++)
    {
    for(int j = 0; j< i; j++)
    if(checkkey ==checkkey[j])
    {
    checkkey = '0';
    }
    }

    // what i did here was just assign all repeated b's to 0 so i would
    have
    // bl0a0h


    for(int i = 0; i<pkeysize; i++)
    {
    System.out.println(checkkey);
    if(checkkey == '0')
    counter++;
    }

    // it works up to this point, i get bl0a0h


    // some thing is wrong with the aglorithm here....

    System.out.println(counter);
    int nsize = pkeysize-counter;
    char nkey[] = new char[pkeysize];
    for(int i = 0; i< pkeysize; i++)
    {
    int j = 0;

    if(checkkey != '0')
    {
    nkey[j] = checkkey;
    j++;
    }
    }


    for(int i = 0; i< nsize; i++)
    System.out.print(nkey);

    // but when i try to print out the nkey, it gives me bunch of shit
     
    spidey12345, Feb 8, 2007
    #1
    1. Advertisements

  2. Did you have a question, or were you just
    'keeping us informed' of your progress?

    Andrew T.
     
    Andrew Thompson, Feb 8, 2007
    #2
    1. Advertisements

  3. spidey12345

    spidey12345 Guest

    yeah i have a question, i am not sure what to do when i reach
    "bl0a0h", read my comments the algorithm

    so i am not sure why it's not reading correctly in that part of the
    code

    when i say if(checkkey != '0') , and going through i iterations,
    shouldn't it only set the ones that is not equal to 0 to the indexes,
    why is it still
    setting 0 to the answer

    so when i print out the new key, it gives me bla0 rather than blah for
    some reasons
     
    spidey12345, Feb 8, 2007
    #3
  4. spidey12345

    Oliver Wong Guest

    [code snipped]

    First thing that comes to mind for me is to put the characters in a
    LinkedHashSet, and then pull them out again in the correct order.

    - Oliver
     
    Oliver Wong, Feb 8, 2007
    #4
  5. spidey12345

    spidey12345 Guest

    Does java have any standard libary for this, i am new to java, so i
    delt linkedhashset would be something i like to use.
     
    spidey12345, Feb 8, 2007
    #5
  6. spidey12345

    Oliver Wong Guest

    Yup: http://java.sun.com/javase/6/docs/api/java/util/LinkedHashSet.html

    - Oliver
     
    Oliver Wong, Feb 8, 2007
    #6
  7. spidey12345

    spidey12345 Guest

    ok, fine, i am using hashset:)

    but which function in there allow me to delete repeated values or 0,
    that is not character
     
    spidey12345, Feb 9, 2007
    #7
  8. spidey12345

    Lew Guest

    add()

    Read the documentation for Set:
    <http://java.sun.com/javase/6/docs/api/java/util/Set.html>

    You will note the very first phrase of the explanation for Set: "A collection
    that contains no duplicate elements."

    The "0" was an artifact of your first approach and not part of the original
    problem definition.

    - Lew
     
    Lew, Feb 9, 2007
    #8
  9. Why do you use an array? You don't even need a HashSet if speed doesn't
    matter.

    Untested (I even didn't tried to compile it) code:

    public String removeDuplicateLetters( String word ) {
    if ( word == null )
    return null;

    StringBuilder builder = new StringBuilder();
    for ( int i = 0, n = word.length(); i < n; i++ ) {
    String sub = word.substring(i,i+1);
    if ( builder.indexOf(sub) == -1 )
    builder.append(sub);
    }

    return builder.toString();
    }

    If you want to use a HashSet due to performance reasons the only thing
    you'd have to replace is the if-condition and of course, you'd have to
    add the element to the Set.

    Bye
    Michael
     
    Michael Rauscher, Feb 9, 2007
    #9
  10. spidey12345

    Bart Rider Guest



    Move this statement out of the loop, than it will behave
    like you want.


    Hint: form a string out of the new char-array and print this.
    Think about it: what happens if your word contains the character
    '0'? Maybe you should consider using '\0' instead of '0'.

    Best regards,
    Bart
     
    Bart Rider, Feb 12, 2007
    #10
  11. spidey12345

    Daniel Pitts Guest

    This might be a little better:
    Its known to compile and run, for one thing.
    public class Test2 {
    public static void main(String[] args) {
    final String s = "My string has a lot of duplicate letters";
    final StringBuilder builder = new StringBuilder(s);
    for (int i = 0; i < builder.length(); ++i) {
    final String charStr =
    Character.toString(builder.charAt(i));
    int index = builder.indexOf(charStr, i+1);
    while (index > i) {
    builder.deleteCharAt(index);
    index = builder.indexOf(charStr, index);
    }
    }
    System.out.println(builder.toString());
    }
    }
     
    Daniel Pitts, Feb 12, 2007
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.