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. Advertising

  2. On Feb 9, 1:08 am, "spidey12345" <> wrote:
    > so If i enter the letter blbabh

    ....
    > // but when i try to print out the nkey, it gives me bunch of shit


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

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

  3. spidey12345

    spidey12345 Guest

    On Feb 8, 10:17 am, "Andrew Thompson" <> wrote:
    > On Feb 9, 1:08 am, "spidey12345" <> wrote:
    >
    > > so If i enter the letter blbabh

    > ...
    > > // but when i try to print out the nkey, it gives me bunch of shit

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


    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

    "spidey12345" <> wrote in message
    news:...
    > so If i enter the letter blbabh
    >
    > the word will acutally become: blah

    [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

    On Feb 8, 12:48 pm, "Oliver Wong" <> wrote:
    > "spidey12345" <> wrote in message
    >
    > news:...> so If i enter the letter blbabh
    >
    > > the word will acutally become: blah

    >
    > [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


    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

    "spidey12345" <> wrote in message
    news:...
    > On Feb 8, 12:48 pm, "Oliver Wong" <> wrote:
    >> "spidey12345" <> wrote in message
    >>
    >> news:...> so If i
    >> enter the letter blbabh
    >>
    >> > the word will acutally become: blah

    >>
    >> [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

    >
    > Does java have any standard libary for this, i am new to java, so i
    > delt linkedhashset would be something i like to use.


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

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

    spidey12345 Guest

    On Feb 8, 2:06 pm, "Oliver Wong" <> wrote:
    > "spidey12345" <> wrote in message
    >
    > news:...
    >
    >
    >
    >
    >
    > > On Feb 8, 12:48 pm, "Oliver Wong" <> wrote:
    > >> "spidey12345" <> wrote in message

    >
    > >>news:...> so If i
    > >> enter the letter blbabh

    >
    > >> > the word will acutally become: blah

    >
    > >> [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

    >
    > > Does java have any standard libary for this, i am new to java, so i
    > > delt linkedhashset would be something i like to use.

    >
    > Yup:http://java.sun.com/javase/6/docs/api/java/util/LinkedHashSet.html
    >
    > - Oliver- Hide quoted text -
    >
    > - Show quoted text -

    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

    spidey12345 wrote:
    > ok, fine, i am using hashset:)
    >
    > but which function in there allow me to delete repeated values or 0,
    > that is not character


    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. spidey12345 wrote:
    > ok, fine, i am using hashset:)
    >
    > but which function in there allow me to delete repeated values or 0,
    > that is not character


    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

    spidey12345 wrote:
    > 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;


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

    >
    > if(checkkey != '0')
    > {
    > nkey[j] = checkkey;
    > j++;
    > }
    > }
    >
    >
    > for(int i = 0; i< nsize; i++)
    > System.out.print(nkey);


    Hint: form a string out of the new char-array and print this.

    >
    > // but when i try to print out the nkey, it gives me bunch of shit
    >


    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

    On Feb 9, 4:10 am, Michael Rauscher <> wrote:
    > spidey12345 wrote:
    > > ok, fine, i am using hashset:)

    >
    > > but which function in there allow me to delete repeated values or 0,
    > > that is not character

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


    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. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. srinivas

    Auto letters (word ) from ASp.NET

    srinivas, Jun 30, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    473
    srinivas
    Jul 1, 2003
  2. Merrigan
    Replies:
    4
    Views:
    583
    Chris
    Dec 14, 2007
  3. David Segall
    Replies:
    9
    Views:
    541
    Joy Beeson
    May 1, 2010
  4. Venugopal
    Replies:
    11
    Views:
    1,561
    Tassilo v. Parseval
    Nov 5, 2003
  5. Sandfordc
    Replies:
    11
    Views:
    201
    Random
    Jun 3, 2005
Loading...

Share This Page