Return数组中唯一数值的个数"arr"

Return the number of unique number values in the array "arr"

我正在寻找一种方法来查找数组中唯一数值的数量。

我不能为这个项目使用导入,但我可以使用循环和条件语句。

例如数组

int[] arr1 = {3, -3, -1,  0, -1,  4,  0,  3,  4,  0};

应该return5

这是我到目前为止的想法:

public static int countUniqueIntegers(int[] arr){
    
    // Initialize int "counter" with value 0
    int num_unique = 0;
    
    // TO DO: Add logic to count unique values
    if(arr.length == 0) return 0;
    if(arr.length == 1) return 1;
    double currentNumber = arr[0];
    int currentCount =1;
    for(int i =1; i < arr.length; i++)
    {
      if(arr[i] != currentNumber)
      {
        currentCount++;
        currentNumber = arr[i];
      }
    }
    // Return number of unique values
    return num_unique;
  }

我们可以假设数组中的每个值都是唯一的。所以开始时唯一值的数量与数组长度相同。 在此之后,我们必须将数组中的每个值与同一数组中的每个其他值进行比较。为此,您需要在“for”循环中使用另一个“for”循环。如果来自外部(第一个)循环的当前项目等于来自内部循环的某个项目,您只需从具有唯一数字计数的变量中减去 1(即开头的数组长度)。
您现在要做的就是对其进行编程:)