简体   繁体   English

从 List 中检索所有值对<integer></integer>

[英]Retrieve all value pairs from List<Integer>

For example I have a List<Integer> object with the following:例如,我有一个List<Integer> object 具有以下内容:

3, 6, 5, 3, 3, 6

The result would be 3 and 6. How can I create a function that tests for duplicates and then returns 1 value of the duplicate (not the pair, just one in the pair)?结果将是 3 和 6。如何创建一个 function 来测试重复项,然后返回重复项的 1 个值(不是一对,只是一对中的一个)? One problem that might occur is if there are quadruple values: 3, 4, 5, 3, 8, 3, 3 Then I would like to return 3 and 3. How can I accomplish this?可能出现的一个问题是,如果有四个值:3、4、5、3、8、3、3 那么我想返回3, 4, 5, 3, 8, 3, 3和 3。我该如何完成呢?

I would go through the list counting the number of instances of each one (storing them in a map) and then create a new list from the map:我会 go 通过列表计算每个实例的数量(将它们存储在地图中),然后从 map 创建一个新列表:

List<Integer> values = // the list of values you have
Map<Integer,Integer> counts = new HashMap<Integer,Integer>();

for(Integer value : values) {
    if(counts.containsKey(value)) {
        counts.put(value, counts.get(value)+1);
    } else {
        counts.put(value, 1);
    }
}

List<Integer> resultValues = new ArrayList<Integer>();
for(Integer value : counts.keySet()) {
    Integer valueCount = counts.get(value);
    for(int i=0; i<(valueCount/2); i++) { //add one instance for each 2
        resultValues.add(value);
    }
}
return resultValues;

This avoids the O(nlogn) behavior of sorting the values first, working in O(n) instead.这避免了首先对值进行排序的 O(nlogn) 行为,而是在 O(n) 中工作。

I think the "map" way by @RHSeeger is good enough.我认为@RHSeeger 的“地图”方式已经足够好了。 Here I just suggest another way, just for 'fun', so that u may take a look.在这里我只是建议另一种方式,只是为了“有趣”,以便您可以看看。 It kind of give a stable result: first completed pairs appears first:它给出了一个稳定的结果:第一个完成的对首先出现:

List<Integer> values = ....;
List<Integer> result = new ArrayList<Integer>();
Set<Integer> unpairedValues = new HashSet<Integer>();

for (int i : values) {
  if (unpairedValues.contains(i)) {
    result.add(i);
    unpairedValues.remove(i);
  } else {
    unpairedValues.add(i);
  }
}
// result contains what u want
// ArrayList<Integer> list = {3, 6, 5, 3, 3, 6}
Collections.sort(list);

ArrayList<Integer> pairs = new ArrayList<Integer>();
int last = Integer.MIN_VALUE;  // some value that will never appear in the list
for (Integer cur : list) {
    if (last == cur) {
        pairs.add(cur);
        last = Integer.MIN_VALUE;  // some value that will never appear in the list
    } else {
        last = cur;
    }
}
System.out.println(Arrays.toString(pairs.toArray()));

Will output将 output

[3, 6]

** Edit ** **编辑**

Slightly better algorithm, modifies the given list稍微好一点的算法,修改给定的列表

// ArrayList<Integer> list = {3, 6, 5, 3, 3, 6}
Collections.sort(list);
int index = 1, last;
while (index < list.size()) {
    last = list.remove(index - 1);
    if (list.get(index - 1) == last) {
        index++;
    }
    if (index == list.size()) {
        list.remove(index - 1);
    }
}

One possible way in pseudo code:伪代码中的一种可能方式:

sortedList = sort (list)
duplicates = empty list
while (length (list) > 1)
{
    if (list [0] == list [1] )
    {
         duplicates.append (list [0] )
         list.removeAt (0)
    }
    list.removeAt (0);
}

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

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