我正在使用java语言编写一个函数,它接受一维数组,并将数组的大小作为函数的输入。我想知道数组中有多少个函数值。我该怎么做?
答案 0 :(得分:2)
方法1( O(nlogn)):
方法2( O(n)但 O(n)的空间复杂度):
答案 1 :(得分:1)
#Find unique items from array:
1. Create one new array
2. Take each item from existing array
3. Check if the item is exist in new array
4. **If not exist push the item into new array** else go for next item
5. After iterating all item in array get the length of new array
答案 2 :(得分:0)
#include <stdio.h>
int main ()
{
int n[10] = {1,2,5,5,3,4,1,4,5,11};
int count = 0; int i = 0;
for (i=0; i< 10; i++)
{
int j;
for (j=0; j<i; j++)
if (n[i] == n[j])
break;
if (i == j)
count += 1;
}
printf("The counts are: %d distinct elements", count);
return 0;
}