Thread: How can I count the total no of unique elements in an array?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    45

    How can I count the total no of unique elements in an array?

    Hi
    How can i count the total no of unique elements in an array?
    like I have an array.

    Code:
    int array[]= { 2,1,4,0,3,3,0,0,1,2,1,1}
     // As it has 0,1,2,3,4 as unique values so total no of unique values are=5
    int unique =5;
    Thanks

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    What have you tried?

    Jim

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    45
    My problem is that array is not sorted, it work fine if i do first sorting.. some thing like that


    1. Code:
       const int size = 7;
    2. int array[size] = {0,0,0,1,2,3,3};
    3. int unique = 1; //In case we have only one element; it is unique!
    4. for(int i = 0; i < size -1 ; i++)
    5. {
    6. if(array[i]==array[i+1])
    7. continue;
    8. else
    9. unique++;
    10. }
    11. printf("The number of unique elements are %d ",unique);
    12. }


Quote Originally Posted by jimblumberg View Post
What have you tried?

Jim
Last edited by gevni; 03-19-2013 at 08:35 AM.

  • #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,776
    Quote Originally Posted by gevni
    My problem is that array is not sorted, it work fine if i do first sorting.
    That is a good idea, so do it.

    By the way, post your code in [code][/code] bbcode tags instead of list tags.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  • #5
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    So if the array is not sorted then either sort the array, or maybe create an array of numbers found to store the "unique" numbers. Also why are you using "i < size -1" in you for loop, that will probably miss the last element.

    Jim

  • Popular pages Recent additions subscribe to a feed

    Similar Threads

    1. Sort and count frequency of array elements.
      By Nurlana in forum C++ Programming
      Replies: 12
      Last Post: 10-25-2012, 11:50 PM
    2. Replies: 2
      Last Post: 05-06-2011, 07:43 AM
    3. Unique Word count program
      By vjefcoat in forum C Programming
      Replies: 3
      Last Post: 11-22-2010, 06:51 PM
    4. count how many elements used in calloc
      By dunsta in forum C Programming
      Replies: 3
      Last Post: 05-05-2010, 02:17 AM
    5. count elements in an array
      By whichet in forum C Programming
      Replies: 9
      Last Post: 11-25-2007, 08:05 AM