我需要最快和最简单的算法来找到数组中的重复数字,还应该能够知道重复的数量。
例如:如果数组是{2,3,4,5,2,4,6,2,4,7,3,8,2}
我应该能够知道有四个 2、两个 3 和三个 4。
最佳答案
创建一个哈希表,其中键为数组项,值为对应数组项在数组中出现的次数的计数器。这是一种有效的方法,但可能不是最快的方法。
类似这样的东西(伪代码)。你会find plenty of hash map implementations for C by googling .
hash_map = create_new_hash_map()
for item in array {
if hash_map.contains_key(item){
counter = hash_map.get(item)
} else {
counter = 0
}
counter = counter + 1
hash_map.put(item, counter)
}
关于c - 查找数组中重复数字的算法---Fastest Way,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1851716/