delete repeated letters in a word

  • Follow


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[i] ==checkkey[j])
		{
		  checkkey[i] = '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[i]);
		if(checkkey[i] == '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[i] != '0')
		{
			nkey[j] = checkkey[i];
			j++;
		}
	}


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

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

0
Reply swq_shan10 (14) 2/8/2007 2:08:13 PM

On Feb 9, 1:08 am, "spidey12345" <swq_sha...@hotmail.com> 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.

0
Reply andrewthommo (2516) 2/8/2007 3:17:19 PM


On Feb 8, 10:17 am, "Andrew Thompson" <andrewtho...@gmail.com> wrote:
> On Feb 9, 1:08 am, "spidey12345" <swq_sha...@hotmail.com> 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[i] != '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

0
Reply swq_shan10 (14) 2/8/2007 5:35:42 PM

"spidey12345" <swq_shan10@hotmail.com> wrote in message 
news:1170943693.826943.308510@a34g2000cwb.googlegroups.com...
> 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 


0
Reply owong (5281) 2/8/2007 5:48:21 PM

On Feb 8, 12:48 pm, "Oliver Wong" <o...@castortech.com> wrote:
> "spidey12345" <swq_sha...@hotmail.com> wrote in message
>
> news:1170943693.826943.308510@a34g2000cwb.googlegroups.com...> 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.

0
Reply swq_shan10 (14) 2/8/2007 6:58:31 PM

"spidey12345" <swq_shan10@hotmail.com> wrote in message 
news:1170961111.387359.39480@l53g2000cwa.googlegroups.com...
> On Feb 8, 12:48 pm, "Oliver Wong" <o...@castortech.com> wrote:
>> "spidey12345" <swq_sha...@hotmail.com> wrote in message
>>
>> news:1170943693.826943.308510@a34g2000cwb.googlegroups.com...> 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 


0
Reply owong (5281) 2/8/2007 7:06:07 PM

On Feb 8, 2:06 pm, "Oliver Wong" <o...@castortech.com> wrote:
> "spidey12345" <swq_sha...@hotmail.com> wrote in message
>
> news:1170961111.387359.39480@l53g2000cwa.googlegroups.com...
>
>
>
>
>
> > On Feb 8, 12:48 pm, "Oliver Wong" <o...@castortech.com> wrote:
> >> "spidey12345" <swq_sha...@hotmail.com> wrote in message
>
> >>news:1170943693.826943.308510@a34g2000cwb.googlegroups.com...> 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

0
Reply swq_shan10 (14) 2/9/2007 4:33:41 AM

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
0
Reply lew7583 (253) 2/9/2007 7:04:11 AM

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
0
Reply michlmann (357) 2/9/2007 12:10:06 PM

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

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
0
Reply caretaker (24) 2/12/2007 5:15:01 PM

On Feb 9, 4:10 am, Michael Rauscher <michlm...@gmx.de> 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());
    }
}

0
Reply googlegroupie (586) 2/12/2007 9:22:32 PM

10 Replies
17 Views

(page loaded in 0.156 seconds)


Reply: