Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By SurfMan

Thread: How to find duplicate words in string array?

  1. #1
    chkontog is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default 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

  2. #2
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,006
    Rep Power
    3

    Default 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.
    Last edited by SurfMan; 11-12-2012 at 12:16 AM.
    Fubarable likes this.

  3. #3
    chkontog is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: How to find duplicate words in string array?

    Ok, thanks ;-)

    Quote Originally Posted by SurfMan View Post
    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.

Similar Threads

  1. find index of duplicate words inside string
    By onions in forum New To Java
    Replies: 9
    Last Post: 05-09-2012, 04:48 PM
  2. Need to find a way to print line spacing by words
    By technoreject in forum New To Java
    Replies: 1
    Last Post: 07-07-2011, 03:43 PM
  3. find the link between two words by using wordnet
    By STR in forum Advanced Java
    Replies: 8
    Last Post: 07-06-2011, 08:59 AM
  4. using loop to find duplicate
    By gwithey in forum New To Java
    Replies: 7
    Last Post: 03-06-2009, 02:46 PM
  5. random string are duplicate
    By googgoo in forum New To Java
    Replies: 3
    Last Post: 04-03-2008, 11:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •