找到一个数组中所发生的次数奇数的所有元素 [英] Find all the elements in an array which occur odd number of times

查看:176
本文介绍了找到一个数组中所发生的次数奇数的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题:

查找数组中的其中出现次数奇数的所有元素。

'Find all the elements in an array which occur odd number of times'.

我的这个想法是:

  1. 使用的HashMap :添加值的数组作为HashMap的键研究。对应于每个键的值将是倍键遇到号

  1. Use HashMap: Add values in the array as keys in the HashMap. The value corresponding to each key will be number of times that key is encountered.

使用快速排序为O排序阵列(N日志N),然后通过数组遍历检查哪些元素出现次数奇数。

Sort the array using Quick sort in O(N log N) and then traverse through the array to check which elements occur odd number of times.

你怎么想,有没有其他的方法呢?如果没有,那么其中这2个方法比较好?

What do you think, is there any other approach to this? If no, then which of these 2 approaches is better?

在此先感谢!

推荐答案

更好取决于上下文。使用散列映射或哈希集将更快,也没有修改原始数组的优势,但它需要O(N),额外的内存。分类和计数需要更长的时间,并修改该数组,但不需要额外的内存。

"Better" depends on context. Using the hash map or hash set will be faster and has the advantage of not modifying the original array, but it requires O(N) extra memory. Sorting and counting takes longer and modifies the array, but requires no extra memory.

哪种解决方案选择取决于你是否能够承受额外的内存。

Which solution you choose depends on whether you can afford the extra memory.

这篇关于找到一个数组中所发生的次数奇数的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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