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

时间:2015-03-03 01:13:08

标签: java arrays int duplicates duplicate-removal

问题是如标题所示,我如何摆脱整数数组中的重复值?我希望拥有它,以便用户输入五个数字,所有数字都在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]。我被困在如何做到这一点。另外,我已经编辑了如果数字介于10和100之间,如果声明更容易阅读,并且到达手头的主要部分。

5 个答案:

答案 0 :(得分:2)

使用套装怎么样?特别是LinkedHashSet允许您在O(n)时间和内存中执行此操作,同时保留输入的顺序。在你说'#34;但我不能使用HashSet之前,&#34;你需要它的行为以获得最佳解决方案,因此后续问题可能是&#34;我如何实现LinkedHashSet,可能将逻辑融入我的程序?&#34;

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;
    }
}

答案 1 :(得分:0)

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

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

另一种方法是使用Set Collection。将您收到的每个号码添加到Set的实例,最后将Set转换为Array。默认情况下,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
}

答案 2 :(得分:0)

你说你最后想要一个可变数量的数字,所以这里的数组不是正确的选择,而是使用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.");
    }
}

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

我是在没有电脑的情况下写的,所以我可以混淆一些东西,但你会得到一般的想法。

答案 3 :(得分:0)

一个班轮可以达到你想要的效果:

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

阅读完值并将其存储在intArray

后,请执行此操作

如果您想保留输入值的顺序,请使用LinkedHashSet,否则只使用HashSet

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

答案 4 :(得分:-1)

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

  2. 使用地图商店输入值作为键。在保存数组中的值之前,请检查键是否在映射中。

相关问题