我有一个字符串数组,我想在数组中找到重复的字符串,并希望通过使用具有良好时间复杂度的 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/