gpt4 book ai didi

java - 如何使用 HashMap 查找字符串数组的重复条目并使它们为空

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:33:36 25 4
gpt4 key购买 nike

我有一个字符串数组,我想在数组中找到重复的字符串,并希望通过使用具有良好时间复杂度的 HashMap 使重复的字符串为空。

最佳答案

听起来你想使用 Set。这会清除所有重复的条目,但您也可以只创建一个具有唯一条目(并且没有空值)的数组

String[] array =
Set<String> found = new LinkedHashSet<String>();
for(int i=0;i<array.length;i++)
if(!found.add(array[i]))
array[i] = null;

// just the entries without duplicates.
String[] unique = found.toArray(new String[found.size()]);

关于java - 如何使用 HashMap 查找字符串数组的重复条目并使它们为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6262594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com