Thread: Please Help!

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    5

    Please Help!

    Question: How to find a duplicate numbers and numbers found once in array. Any Example will be a big help. Thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is an essential trick to learn:

    You want to find what the count is for each digit from one to 7, in the following numbers:

    3, 1, 5, 2, 3, 6, 1

    And you have 8 cups lined up in front of you, on a table. Your plan is to look at the number, and every time you see a number, you're going to put a piece of penny, into the cup: a 1 causes a penny to go into the 1's cup, a 2, into the 2's cup, a 3 into the 3's cup, etc.

    You see a 3, so you put a penny into the third cup, next a 1, so you put a penny into the 1's cup, etc. Until all the numbers have been gone through.

    Now you count the penny's in each cup, and they look like this:
    0's cup: 0
    1's cup: 2
    2's cup: 1
    3's cup: 2
    4's cup: 0
    5's cup: 1
    6's cup: 1
    7's cup: 0

    For your program, you could use an array for the numbers, and use another int array for the cups.

    Now in a for loop in your program, you could loop at the value in the cups array, and see which numbers were unique (had a 1), and which values were duplicated (had a 2).

    The key to doing that is using the assignment, inside a loop:
    cups[number]++;

    So each time you see a number (whatever the number might be), the number serves as the index to the cups array, and that element of the cups array: cups[number], is incremented by one.

    This trick doesn't work well with huge numbers, but most of the problems we deal with, involve numbers that work very well with this.

    Try and code that up, and if you have a problem, post it and tell us what the problem is.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,795
    Grab a big piece of paper and packet of this: Alphabet Pasta, Bulk, 16 oz: Amazon.com: Grocery & Gourmet Food

    Spread a handful of the noodles (pretend they're numbers) over the big piece of paper. Now, the noodles are your values and the big piece of paper is the array. How would you go about finding duplicate shapes and those that occur only once?

    Now, to make the paper more like an array, get an empty egg carton and put one noodle in each cell. How would you then go about the task?

    Edit: Just saw Adak's reply. Yeah get a bunch of cups and number them, putting one random noodle shape in each cup.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Pasta! I should've thought of pasta noodles!!

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    5
    ok , thanks guys for the examples. I understand the idea how to do it. I know I ask too much sorry brothers, I just need a code example so I can follow. Thanks again for replaying

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No, you THINK you understand the examples we've shown, but you REALLY don't. If you actually DID them a few times, you'd see the light, and realize how SIMPLE it is to code them up - at least to TRY.

    If you aren't willing to even TRY then you need to hire someone.

  7. #7
    Registered User
    Join Date
    Nov 2013
    Posts
    5
    Sorry brother , I believe you understand me wrong, I really appreciated your information, I try so many time, but today I figure it. My program is working good. Thanks for your time and replying.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I did misunderstand you - well done, Ismael!

Popular pages Recent additions subscribe to a feed