Hi
How can i count the total no of unique elements in an array?
like I have an array.
ThanksCode:
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;
Printable View
Hi
How can i count the total no of unique elements in an array?
like I have an array.
ThanksCode:
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;
What have you tried?
Jim
My problem is that array is not sorted, it work fine if i do first sorting.. some thing like that
Code:const int size = 7;
- int array[size] = {0,0,0,1,2,3,3};
- int unique = 1; //In case we have only one element; it is unique!
- for(int i = 0; i < size -1 ; i++)
- {
- if(array[i]==array[i+1])
- continue;
- else
- unique++;
- }
- printf("The number of unique elements are %d ",unique);
- }
That is a good idea, so do it.Quote:
Originally Posted by gevni
By the way, post your code in [code][/code] bbcode tags instead of list tags.
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