Counting unique elements in an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aszia787
    New Member
    • Jul 2007
    • 3

    #1

    Counting unique elements in an array

    Hi guys,

    I need to count the unique elements in an array. For example, if i have an array,
    array[0,0,0,1,2,3,3]. The number of unique elements in the array is 3. I am sorry if this message has been posted before. Help is appreciated. Thanks in advance.

    Regards,

    AZ.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by aszia787
    Hi guys,

    I need to count the unique elements in an array. For example, if i have an array,
    array[0,0,0,1,2,3,3]. The number of unique elements in the array is 3. I am sorry if this message has been posted before. Help is appreciated. Thanks in advance.

    Regards,

    AZ.
    Is the array sorted, i.e. are the non-unique elements adjacent to eachother?
    (that was a spoiler already, I'm sure).

    kind regards,

    Jos

    Comment

    • aszia787
      New Member
      • Jul 2007
      • 3

      #3
      Yes, the arrays are sorted. Thanks for your reply.

      AZ.

      Comment

      • seforo
        New Member
        • Nov 2006
        • 60

        #4
        Actually there are 4 unique elements in your example array, zero is also a number. Since the numbers are sorted then it is simple:
        [CODE=c++]
        #include <iostream>
        using namespace std;
        int main()
        {
        const int size = 7;
        int array[size] = {0,0,0,1,2,3,3} ;
        int unique = 1; //incase we have only one element; it is unique!
        for(int i = 0; i < size -1 /*since we don't want to compare last element with junk*/; i++)
        {
        if(array[i]==array[i+1])
        continue;
        else
        unique++;
        }
        cout<<"The number of unique elements is "<<unique<<endl ;
        return 0;
        }
        [/CODE]

        I hope I have not broken the laws of the scripts by giving you a fully code.

        Comment

        • aszia787
          New Member
          • Jul 2007
          • 3

          #5
          Originally posted by seforo
          Actually there are 4 unique elements in your example array, zero is also a number. Since the numbers are sorted then it is simple:
          [CODE=c++]
          #include <iostream>
          using namespace std;
          int main()
          {
          const int size = 7;
          int array[size] = {0,0,0,1,2,3,3} ;
          int unique = 1; //incase we have only one element; it is unique!
          for(int i = 0; i < size -1 /*since we don't want to compare last element with junk*/; i++)
          {
          if(array[i]==array[i+1])
          continue;
          else
          unique++;
          }
          cout<<"The number of unique elements is "<<unique<<endl ;
          return 0;
          }
          [/CODE]

          I hope I have not broken the laws of the scripts by giving you a fully code.
          Hi,

          Thanks bro. Thx for your help. highly appreciated.

          AZ.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by seforo
            I hope I have not broken the laws of the scripts by giving you a fully code.
            Well actually you have, but apparently it slipped through. Don't post full code
            spoonfeeding solutions anymore. Read the Help link (top right) for the forum
            posting guidelines. When I happen to pass by here and I see full code spoon-
            feeding, I remove it; please don't do it again, i.e. it is not in the benefit of the OP
            to cheat with homework assignments.

            kind regards,

            Jos

            Comment

            Working...