[英]Remove each element that occrus more than 'N' times problem
I'm kind of stuck with a small bug in my code and I hope you could help me!我有点被代码中的一个小错误困住了,我希望你能帮助我!
I have an ArrayList and I want to remove all the elements that repeat more than 'N' times.我有一个 ArrayList,我想删除所有重复次数超过“N”次的元素。 My solution works just fine in most cases, but when it comes to N=1 and ArrayList size is more than 30 or so it skips one element so it's not unique.我的解决方案在大多数情况下都可以正常工作,但是当 N=1 且 ArrayList 大小超过 30 左右时,它会跳过一个元素,因此它不是唯一的。 Its index is random( i mean I tried to find a special index where the problem is hidden, but I could not).它的索引是随机的(我的意思是我试图找到一个隐藏问题的特殊索引,但我找不到)。
Here comes the code代码来了
while(isSorted!=true) {
for (int i = 0; i < test.size(); i++) {
counter = 0;
temp = test.get(i);
for (int j = i + 1; j < test.size(); j++) {
if (temp == test.get(j)) {
counter++;
if (counter >= maxOccurrences) {
test.remove(j);
}
}
}
if(counter<maxOccurrences){
isSorted=true;
}
}
}
If I just repeat this block of code one more time it deletes this element and everything is fine.如果我再次重复这段代码,它会删除这个元素,一切都很好。 I'm trying to understand why it would skip one not unique element.我试图理解为什么它会跳过一个不唯一的元素。 I could use HashSet for this particular case(when N=1) but it feels like cheating a little bit.我可以将 HashSet 用于这种特殊情况(当 N=1 时),但感觉有点作弊。 I hope I was clear with my question and provided all information that is needed.我希望我的问题很清楚,并提供了所有需要的信息。 Thank you in advance.先感谢您。 Peace and love!和平与爱!
Messing with a List while you're iterating over it can be hazardous to your health.在迭代列表时弄乱它可能对您的健康有害。 I suspect that your index-based loops are getting fooled by calls to remove
, which cause shifts.我怀疑您的基于索引的循环被调用remove
所愚弄,这会导致移位。
Your algorithm is something on the order of N^3.您的算法大约是 N^3。 Consider sorting the array first if you're allowed to do so, or using additional storage to reduce search times by counting all the array elements in a single pass as suggested by another respondent.如果您被允许,请考虑先对数组进行排序,或者按照另一位受访者的建议,通过一次性计算所有数组元素来使用额外的存储空间来减少搜索时间。
It seems to me that using HashSet would be better in terms of asymptotics, since it would not exceed O(n) by the sum rule.在我看来,就渐近性而言,使用 HashSet 会更好,因为它不会超过求和规则的 O(n)。
Map<Integer, Integer> newMap = new HashMap<>();
integers.forEach(integer -> {
if(newMap.containsKey(integer) && integer != null)
newMap.put(integer, newMap.get(integer) + 1);
else
newMap.put(integer, 1);
});
return newMap.entrySet()
.stream()
.filter(x->x.getValue().equals(numberOfDuplicates))
.map(x->x.getKey())
.collect(Collectors.toList())
And what about your code it is also better to use equals()
instead of ==
, because if the numbers are greater than 127
and less than -127
, your code will always work incorrectly.对于您的代码,最好使用equals()
而不是==
,因为如果数字大于127
且小于-127
,您的代码将始终无法正常工作。
The problem is that with remove
, you need to reduce j
by 1, because if the elements go in a row, the next element will move to the past j
, and you won't delete it, because you're already on j+1
element问题是使用remove
,你需要将j
减 1,因为如果元素 go 在一行中,下一个元素将移动到过去的j
,你不会删除它,因为你已经在j+1
元素
if (counter >= maxOccurrences) {
test.remove(j);
j--;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.