如何使用Java从列表中移除重复项并保留原始元素

huangapple go评论108阅读模式
英文:

How to remove duplicates and matching original elements from list using java

问题

我已经尝试过使用Set集合来移除重复项,但这并不能解决我在这里提出的问题。

我正在尝试完成以下任务:我有一个列表
Integer [] list1 = {1,2,3,3};
我想要移除重复项,但我还想移除匹配的成对项,所以我想要的结果是
{1,2}

英文:

I have already tried using a Set Collection to remove duplicates but this does not solve what i am asking here.

I am trying to accomplish the following task: I have a list
Integer [] list1 = {1,2,3,3};
i want to remove the duplicates but also I want to remove the matching pair, so the result i want is
{1,2}

答案1

得分: 3

我会使用频率映射来筛选出频率大于2的元素。

  • 频率映射 freqMap 的条目为:{1=1, 2=1, 3=2},我们只需要筛选出值大于2的条目 .filter(e -> e.getValue() < 2),在这里是 {3=2}
  • 现在我们有了条目:{1=1, 2=1},但我们只想要键 {1, 2},所以我们使用 map() 函数来获取键:.map(e -> e.getKey())

尝试这样做:

  1. Map<Integer, Long> freqMap = Arrays.stream(list1)
  2. .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
  3. int[] result = freqMap.entrySet().stream()
  4. .filter(e -> e.getValue() < 2).map(e -> e.getKey()).mapToInt(m -> m).toArray();
  5. System.out.println(Arrays.toString(result));

输出:

  1. [1, 2]
英文:

I would use a frequency map to filter the elements having a frequency greater than 2.

  • The frequency map freqMap has the entries: {1=1, 2=1, 3=2}, we just need to filter the entries having a value greater than 2 .filter(e -> e.getValue()<2) which is {3=2} here.
  • Now we have the entries: {1=1, 2=1} But we want only the keys {1, 2} so we use map() function to get the keys: .map(e -> e.getKey())

Try this:

  1. Map<Integer, Long> freqMap = Arrays.stream(list1)
  2. .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
  3. int[] result = freqMap.entrySet().stream()
  4. .filter(e -> e.getValue() < 2).map(e -> e.getKey()).mapToInt(m -> m).toArray();
  5. System.out.println(Arrays.toString(result));

Output:

  1. [1, 2]

答案2

得分: 1

你可以使用一个Map。计算元素在一个<int, int>的映射中出现的次数,然后只获取那些计数为1的键。

所以,这就是你的示例内容,

{

1: 1,
2: 1,
3: 2,

}

然后只提取键1和2,因为它们的计数为1。时间和空间复杂度为O(n)。

英文:

You can use a Map. Count the number of times the elements appear in a <int, int> map and get only those keys that have a count of 1.

so then this is what you have for your example,

  1. {
  2. 1: 1,
  3. 2: 1,
  4. 3: 2,
  5. }

Then just extract keys 1 and 2 since they have a count of 1. O(n) time and space.

答案3

得分: 0

我的第一个想法是从列表中获取一个集合,然后移除列表中所有在集合中的元素,接着移除集合中所有在剩余列表中的元素。

  1. Integer[] arr = { 1, 2, 3, 3 };
  2. List<Integer> list = new ArrayList<>(Arrays.asList(arr));
  3. Set<Integer> set = new HashSet<>(list);
  4. for (Iterator<Integer> iterator = set.iterator(); iterator.hasNext();) {
  5. list.remove(iterator.next());
  6. }
  7. set.removeAll(list);
  8. System.out.println(list);
  9. System.out.println(set);

输出:

  1. [3]
  2. [1, 2]
  3. 这个方法适用于 Java 8 之前的版本。否则,只需使用 Hülya 给出的 freqMap() 方法。
  4. <details>
  5. <summary>英文:</summary>
  6. My first thought was to get a set from the list, then remove all elements in the list that are in the set, and remove all elements in the set that are in the remaining list.
  7. Integer[] arr = { 1, 2, 3, 3 };
  8. List&lt;Integer&gt; list = new ArrayList&lt;&gt;(Arrays.asList(arr));
  9. Set&lt;Integer&gt; set = new HashSet&lt;&gt;(list);
  10. for (Iterator&lt;Integer&gt; iterator = set.iterator(); iterator.hasNext();) {
  11. list.remove(iterator.next());
  12. }
  13. set.removeAll(list);
  14. System.out.println(list);
  15. System.out.println(set);
  16. Output:
  17. [3]
  18. [1, 2]
  19. This will work with versions of Java less than Java 8. Otherwise just use freqMap() as answered by H&#252;lya.
  20. </details>

huangapple
  • 本文由 发表于 2020年8月26日 00:00:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63582788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定