# How to find duplicate words in string array?

Printable View

• 11-11-2012, 10:59 PM
chkontog
How to find duplicate words in string array?
Hello,

I have an array of strings and i would like to find and count the duplicate words.Do you have any idea for that?

Thanks
• 11-12-2012, 12:12 AM
SurfMan
Re: How to find duplicate words in string array?
I will give you 10 notes with a word on it. I want you to find out how many duplicate notes there are. How would you go about solving this problem?

Try doing this with pen and paper. Here's a start:
- get a new big paper to write stuff down (we'll call this paper a HashMap. You see what I did there?)
- read the first note
- if my paper does *not* contain this word, write it down and write 1 behind it
- if my paper *does* contain this word, take the number that is behind it and add 1 to that
- read the second note
- if my paper does *not* contain this word, write it down and write 1 behind it
- if my paper *does* contain this word, take the number that is behind it and add 1 to that
- read the third note
- if my paper does *not* contain this word, write it down and write 1 behind it
- if my paper *does* contain this word, take the number that is behind it and add 1 to that
- ....

Now that you have counted all the words, you need to go through the paper again, and see which word is listed more than once.

This is not the only way to do this, this is *a* way... There are many more ways, everybody has his preference.

I know I sound like a pedantic bastard, but it really is the way to get your head around problems like this. Write it down, make drawings, flowcharts, whatever works for you. Then convert it to Java sourcecode one step at a time. Once you get into programming, you will be needing less paper for trivial problems like this.
• 11-12-2012, 02:16 AM
chkontog
Re: How to find duplicate words in string array?
Ok, thanks ;-)

Quote:

Originally Posted by SurfMan
I will give you 10 notes with a word on it. I want you to find out how many duplicate notes there are. How would you go about solving this problem?

Try doing this with pen and paper. Here's a start:
- get a new big paper to write stuff down (we'll call this paper a HashMap. You see what I did there?)
- read the first note
- if my paper does *not* contain this word, write it down and write 1 behind it
- if my paper *does* contain this word, take the number that is behind it and add 1 to that
- read the second note
- if my paper does *not* contain this word, write it down and write 1 behind it
- if my paper *does* contain this word, take the number that is behind it and add 1 to that
- read the third note
- if my paper does *not* contain this word, write it down and write 1 behind it
- if my paper *does* contain this word, take the number that is behind it and add 1 to that
- ....

Now that you have counted all the words, you need to go through the paper again, and see which word is listed more than once.

This is not the only way to do this, this is *a* way... There are many more ways, everybody has his preference.

I know I sound like a pedantic bastard, but it really is the way to get your head around problems like this. Write it down, make drawings, flowcharts, whatever works for you. Then convert it to Java sourcecode one step at a time. Once you get into programming, you will be needing less paper for trivial problems like this.