数组大师可以回答 [英] Masters of Array can answer

查看:94
本文介绍了数组大师可以回答的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,其中我存储了多个日期值

现在问题是我必须在同一个数组中找到重复日期

条件:



1)所有值都应该在同一个数组中。

2)数组中的每个值都应与其中的每个值进行比较数组。



谢谢。

i have an array in which i have stored multiple date values
Now the issue is i have to find the duplicate dates in the same array
Conditions :

1) all the values should be in the same array.
2) each value in the array should be compared with every other value in the array.

Thanks.

推荐答案

一个简单的解决方案是循环数组两次。第一个循环将从第一个元素转到第二个最后一个元素(之所以会在一瞬间变得清晰)。第二个循环将从第二个元素转到最后一个元素,并将包含在第一个循环中。



基本上,您将使用外部循环中的值作为内部循环中每个项目的比较。这就是为什么循环有它们的起点和终点。



如伪代码:
A simplistic solution would be to loop through the array twice. The first loop would go from the first element to the second off last element (the reason why will become clear in a moment). The second loop will go from the second element to the last one, and will be contained in the first loop.

Basically, you will use the value in the outer loop as a comparison for every item in the inner loop. This is why the loops have the start and end points that they do.

As pseudo code:
For outerloop = 0 to array.ending_point - 1
  For innerloop = 1 to array.ending_point
    If innerloop.value = outerloop.value Then Duplicate.
  End innerloop
End outerloop



你有逻辑 - 我留给你写代码。



注意 - 有更多有效的方法可以做到这一点,但有些方法不符合你的标准。你可以对元素进行排序,这样你只需要检查下一个元素,看它是否与这个元素相同(那时你只需要一个循环)。您可以从检查中删除重复的条目,以便稍后忽略该匹配的元素,因为它们在循环的早期迭代中找到它们。


You have the logic there - I leave it to you to write the code.

Note - there are many more efficient methods to do this, but some of them don''t match your criteria. You could sort the elements so you only need to check the next element to see if it''s the same as this one (you''d only need one loop then). You could remove duplicate entries from the check so that you would ignore the elements later on that match as they would have been found by earlier iterations through the loop.


向Google询问map-reduce 算法。这将为您提供一种方法来识别数组的所有元素,每次发生的次数,您只需要解析一次数组。
Ask Google about "map-reduce" algorithm. This will give you a way to identify all elements of the array, how many times each occurred and you only have to parse the array once.


这篇关于数组大师可以回答的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆