delete repeated letters in a word

S

spidey12345

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
 
A

Andrew Thompson

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

spidey12345

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
 
O

Oliver Wong

spidey12345 said:
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
 
S

spidey12345

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

Oliver Wong

spidey12345 said:
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
 
S

spidey12345

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
 
L

Lew

spidey12345 said:
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
 
M

Michael Rauscher

spidey12345 said:
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
 
B

Bart Rider

spidey12345 said:
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
 
D

Daniel Pitts

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

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top