如何摆脱 int 数组中的重复值?

How to get get rid of duplicate values in an int array?

问题正如标题所说,如何去除整数数组中的重复值?我想要它,以便用户输入五个数字,所有数字都在 10 到 100 之间。问题是我必须拥有它,这样如果他们输入的值已经输入到数组中,它就不会计数。这是我到目前为止的代码:

public static void main(String[]args){
    Scanner input = new Scanner(System.in);

    int[] intArray = new int[5]; //max of 5 values

    for(int i = 0; i < 5; i++){
        System.out.println("Please enter your desired number that is between 10 and 100: ");
            intArray[i] = input.nextInt(); //puts the value into the array
        }
    System.out.println(Arrays.toString(intArray)); //for test purposes to make sure the array was correctly taking in the values
    }
}


    System.out.println(Arrays.toString(intArray)); //for test purposes to make sure the array was correctly taking in the values

我对如何制作感到困惑,所以如果用户输入数组中已经存在的数字,它就不会添加它。这是我的意思的一个例子,假设用户输入数字 15、22、46、46、77,一旦程序完成循环五次,它将打印出以下内容:[15、22、46、77]。我坚持如何做到这一点。此外,我还删除了 if the number is between 10 and 100 if 语句,以使其更易于阅读,并直达要点。

如果您只需要 select 用户将输入的数字中的唯一数字,最初不要将它们存储在数组中。

相反,将它们存储在 HashMap/Hashtable 中,然后在用户输入完成后将其转换为数组。

另一种方法是使用集合。将您收到的每个数字添加到 Set 的一个实例,最后将 Set 转换为数组。 Set 默认会保持唯一性。

public static void main(String[]args){
    Scanner input = new Scanner(System.in);
    int[] intArray = new int[5]; //max of 5 values
    Set<Integer> uniques = new HashSet<Integer>();
    for(int i = 0; i < 5; i++){
        System.out.println("Please enter your desired number that is between 10 and 100: ");
        uniques.add(input.nextInt()); //puts the value in the set
    }
    System.out.println(Arrays.toString(uniques.toArray())); //for test purposes to make sure the array was correctly taking in the values
}

你是说你想要在末尾有可变数量的数字,所以数组不是这里的正确选择,请改用 ArrayList。

类似于:

List<Integer> intList = new ArrayList<Integer>();
int maxElements = 5; //Set max value here
for(int i = 0; i < maxElements ; i++)
{
    System.out.println("Please enter your desired number that is between 10 and 100: ");
    Integer newNumber = input.nextInt();
    if(newNumber  >= 10 && newNumber  <= 100)
    {
        Boolean found = false;
        foreach (Integer check : intList)
        {
            if (check == newNumber)
            {
                 found = true;
                 break;
            }
        }
        if (!found) intList.Add(newNumber);
    }
    else if(newNumber < 10 || newNumber > 100)
    {
        System.out.println("Enter a valid number next time, this number will not be counted in the results.");
    }
}

重复检查:我添加的那个内部循环检查是否有另一个具有相同值的元素,在这种情况下会跳过你所说的数字。

我在没有电脑的情况下写了这篇文章,所以我可能会混淆一些东西,但你已经明白了大概的意思。

  1. 使数组大小为 100 并使用输入作为索引。在更新 array[input] = input 之前,检查数组的索引(输入编号)的值。

  2. 使用映射存储输入值作为键。在将值保存到数组之前,检查键是否在映射中。

使用 Set 怎么样?特别是 LinkedHashSet,允许您在 O(n) 时间和内存中执行此操作,同时保留输入的顺序。在你说 "But I can't use a HashSet," 之前,你需要它的行为以获得最佳解决方案,所以后续问题可能是 "How would I implement a LinkedHashSet, possibly baking the logic into my program?"

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    Set<Integer> intSet = new LinkedHashSet<>();
    int[] intArray = new int[5]; //max of 5 values

    for (int i = 0; i < 5; i++) {
        System.out.println("Please enter your desired number that is between 10 and 100: ");
        intSet.add(input.nextInt());
    }

    int[] intArray = new int[intSet.size()];

    int i = 0;
    for (Integer val : intSet) {
        intArray[i++] = val;
    }
}

一个班轮将完成你想要的:

Set<Integer> noDuplicates = new LinkedHashSet<>(Arrays.asList(intArray));

在读取值并将它们存储在 intArray 中后执行此操作。

如果要保留输入值的顺序,请使用 LinkedHashSet, otherwise use just a HashSet

一个Set是一个不能有重复的集合。