在Java中计算数组中的不同值 [英] Count different values in array in Java

查看:161
本文介绍了在Java中计算数组中的不同值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个代码,我有一个int [a],该方法应该返回唯一值的数量。示例:{1} = 0个不同的值,{3,3,3} = 0个不同的值,{1,2} = 2个不同的值,{1,2,3,4} = 4个不同的值等。我不允许对数组进行排序

I'm writing a code where I have an int[a] and the method should return the number of unique values. Example: {1} = 0 different values, {3,3,3} = 0 different values, {1,2} = 2 different values, {1,2,3,4} = 4 different values etc. I am not allowed to sort the array.

问题是我的方法可能不起作用。我的声明有问题,我无法弄明白。

The thing is that my method doesn't work probably. There is something wrong with my for statement and I can't figure it out.

public class Program
{

    public static void main(String[] args)
    {
        int[] a = {1, 2, 3, 1};

        System.out.println(differentValuesUnsorted(a));
        //run: 4 //should be 3
    }

public static int differentValuesUnsorted(int[] a)
{
    int values;      //values of different numbers

    if (a.length < 2)
    {
        return values = 0;
    }else if (a[0] == a[1])
    {
        return values = 0;
    }else
    {
        values = 2;
    } 

    int numberValue = a[0];
    for (int i = a[1]; i < a.length; i++)
    {
        if (a[i] != numberValue)
        {
             numberValue++;
             values++;
        }
    }
        return values;
    }
}

有人可以帮忙吗?

推荐答案

这实际上比大多数人想象的要简单得多,这种方法完美无缺:

This is actually much simpler than most people have made it out to be, this method works perfectly fine:

public static int diffValues(int[] numArray){
    int numOfDifferentVals = 0;

    ArrayList<Integer> diffNum = new ArrayList<>();

    for(int i=0; i<numArray.length; i++){
        if(!diffNum.contains(numArray[i])){
            diffNum.add(numArray[i]);
        }
    }

    if(diffNum.size()==1){
            numOfDifferentVals = 0;
    }
    else{
          numOfDifferentVals = diffNum.size();
        } 

   return numOfDifferentVals;
}






让我带你走过它:


Let me walk you through it:

1)提供一个int数组作为参数。

1) Provide an int array as a parameter.

2)创建一个将保持的ArrayList整数:

2) Create an ArrayList which will hold integers:


  • 如果arrayList DOES NOT 包含数组中的整数
    作为a参数,然后将数组参数
    中的元素添加到数组列表中

  • 如果那个arrayList DOES 包含int数组参数中的那个元素,那么没做什么。 (不要将这些值添加到阵列列表中)

  • If that arrayList DOES NOT contain the integer with in the array provided as a parameter, then add that element in the array parameter to the array list
  • If that arrayList DOES contain that element from the int array parameter, then do nothing. (DO NOT ADD THAT VALUE TO THE ARRAY LIST)

注意:这意味着ArrayList包含int []中的所有数字,并删除重复的数字。

N.B: This means that the ArrayList contains all the numbers in the int[], and removes the repeated numbers.

3)ArrayList的大小(类似于数组的长度属性)将是不同值的数量。提供数组。

3) The size of the ArrayList (which is analogous to the length property of an array) will be the number of different values in the array provided.

输入

  int[] numbers = {3,1,2,2,2,5,2,1,9,7};

输出:6

这篇关于在Java中计算数组中的不同值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆