简体   繁体   English

查找不重复的数字

[英]Find non-repeating numbers

I'm trying to find non-repeating numbers in array and print those numbers to new array.我正在尝试在数组中查找非重复数字并将这些数字打印到新数组中。 It's kinda working but prints one number twice for some reason and I don't understand why.它有点工作,但由于某种原因打印了两次一个数字,我不明白为什么。 The output should be 4 and 7 but it prints 4 4 7.输出应该是 4 和 7,但它打印 4 4 7。

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
public class Frequence {
    public static void main(String[] args) {
        int[] array = new int[]
                {
                        1, 2, 3, 2, 3, 3, 5, 4, 7, 1, 5
                };
        List<Integer> arr = new ArrayList<>();
        for (int value : array) {
            int count = 0;
            for (int i : array)
                if (value == i)
                    count++;
            if (count == 1) {
                arr.add(value);
                Integer[] arr2 = new Integer[arr.size()];
                arr2 = arr.toArray(arr2);
                for (Integer k : arr2)
                    System.out.println(k);
            }
        }
    }
}

Since you are using a List<> you might consider using a Set<> .由于您使用的是List<>您可能会考虑使用Set<> Since set values don't contain duplicates and their values are hashed they lend themselves to this type of operation.由于设置值不包含重复项并且它们的值是散列的,因此它们适合这种类型的操作。

The following works with a single loop:以下适用于单个循环:

  • first add the value to the nonDups set.首先将值添加到nonDups集。
  • now add it to the found set.现在把它添加到found集。 If this value is already in that set it was located earlier and returns false.如果该值已在该集合中,则它位于较早的位置并返回 false。 Thus it must be a duplicate so remove it from nonDups .因此它必须是重复的,因此将其从nonDups删除。
  • continue until all values are processed.继续,直到处理完所有值。
Set<Integer> nonDups = new HashSet<>();
Set<Integer> found = new HashSet<>();
for (int k : array) {
    nonDups.add(k);
    if (!found.add(k)) { // returns false if already present
        nonDups.remove(k);
    }
}

System.out.println(nonDups);

Prints印刷

[4,7]

Your print loop prints the whole arr each time unique element is found so it should be outside the for (int value : array) loop:每次找到唯一元素时,您的打印循环都会打印整个 arr,因此它应该在for (int value : array)循环之外:

for (int value : array) {
    // ...
    if (count == 1) {
        arr.add(value);
    }
}

for (Integer k : arr) {
    System.out.println(k);
}

UPD: The simplest approach to find unique element of an array would be using the Set collection. UPD:查找数组唯一元素的最简单方法是使用Set集合。 The whole task can be solved with a one-liner: Set<Integer> uniques = new HashSet(Arrays.asList(array));整个任务可以用一个Set<Integer> uniques = new HashSet(Arrays.asList(array));解决: Set<Integer> uniques = new HashSet(Arrays.asList(array));

Or if you are OK with unmodifiable collection and use Java 9+: Set<Integer> uniques = Set.of(array);或者,如果您对不可修改的集合没问题并使用 Java 9+: Set<Integer> uniques = Set.of(array);

I simplified your code and the problem is you are printing inside the first loop:我简化了你的代码,问题是你在第一个循环中打印:

int[] array = new int[] { 1, 2, 3, 2, 3, 3, 5, 4, 7, 1, 5 };
List<Integer> arr = new ArrayList<>();

for (int i = 0; i < array.length; i++) {
    int count = 0;
    for (int j = 0; j < array.length; j++) {
        if(array[i] == array[j]) {
            count++;
        }
    }
    if(count == 1) {
        arr.add(array[i]);
    }
}
System.out.println(arr.toString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM