Thread: Count distinct array values

  1. #1
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154

    Count distinct array values

    Hi

    I've searched in forums and faq for this but can't find anything useful.
    It's a quick one, I'm pretty sure there's an easy way for doing this (one function most likely);

    I have an array (for example):
    keys[1, 12, 13, 14, 15, 17, 33, 34, 35, 8, 16, 28, 1, 14, 28, 36]

    Now I would like to count how many times each array value is in the array. In this case I need to display only the keys that occur twice in the array (so: 1 and 14).

    Is there an easy way for doing this? Thanks very much in advance.

    René
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    one function most likely
    A function can be as complex as an OS.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by siavoshkc
    A function can be as complex as an OS.
    I meant a standard function that's already defined in some header file. Something like countdistinct(1, keys) (count the amount of times '1' is in the array 'keys')
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I am not sure if such function exists. But you can simply do it yourself if it doesn't.

    First sort the array (There is a standard function to do it), and then count distinct enteries.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by siavoshkc
    I am not sure if such function exists. But you can simply do it yourself if it doesn't.

    First sort the array (There is a standard function to do it), and then count distinct enteries.
    Sorting it first... great idea, thanks.
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to 'replicate' an array of hex values?
    By sef0 in forum C Programming
    Replies: 8
    Last Post: 05-27-2009, 08:53 AM
  2. copying values of an array to another array?!
    By webznz in forum C Programming
    Replies: 8
    Last Post: 10-24-2007, 10:59 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Eliminating duplicate values from a 20x2 array
    By desipunjabi in forum C Programming
    Replies: 2
    Last Post: 10-29-2005, 09:11 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM