Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C JAVA中的数组,重复?_C_Arrays - Fatal编程技术网

C JAVA中的数组,重复?

C JAVA中的数组,重复?,c,arrays,C,Arrays,我正在使用java语言编写一个函数,它接受一维数组和数组大小作为函数的输入。我想知道数组中有多少个函数值。我将如何做到这一点?方法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 arr

我正在使用java语言编写一个函数,它接受一维数组和数组大小作为函数的输入。我想知道数组中有多少个函数值。我将如何做到这一点?

方法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,但空间复杂度为:

为值创建哈希表。 如果哈希表中不存在,请插入一个值。 计算并打印哈希表中存在的值
一个不需要使用数组以外的任何数据结构的选项是,首先对数组进行排序,然后遍历数组,每次遇到新值时都增加一个计数器。另一个选项是,如果可以使用更奇特的数据结构,可以使用映射并遍历数组一次,为每个数字插入相同的值。然后,您可以简单地检查地图中的键数,即不同值的数目。为什么编辑问题以便没有人看到它是什么?为什么删除问题的内容?这不是这个网站的工作方式。您应该将原始问题保留在那里,这样就可以成为堆栈溢出存储库的一部分,该存储库包含以前提出和回答的问题的信息。如果您不想恢复问题的内容,那么我们需要删除它,因为它毫无价值,因为已经没有问题了。将来,您可以通过单击发布的最佳答案旁边的绿色复选标记,向社区表明您的问题已得到回答。这也为你赢得了声望点数。我已经标记了这一点,以引起版主的注意。问题需要回滚到显示问题实际内容的早期版本,或者需要将其删除。我们如何检查现有数组中的项目是否在新数组中?直截了当的是,从新数组中获取每个项目,并将其与现有数组中的当前项目进行比较。谢谢,是的,我使用了另一种方法。哈希太复杂了。我将创建一个二叉树,左节点的值较小,右节点的值较大。不需要处理哈希冲突;可管理的空间和时间要求。
#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;
}