[关闭]
@XQF 2018-03-07T22:57:02.000000Z 字数 547 阅读 1204

如何寻找数组中唯一重复的元素?

数据结构与算法


1.先排序,。,啥都好说了(数值不连续)

  1. public class Solution {
  2. public int findNotSingle(int[] nums) {
  3. if (nums == null || nums.length == 0 || nums.length == 1) {
  4. return -1;
  5. }
  6. Arrays.sort(nums);
  7. for (int i = 0; i < nums.length - 1; i++) {
  8. int temp = nums[i] ^ nums[i + 1];
  9. if (temp == 0) {
  10. System.out.println(nums[i]);
  11. return nums[i];
  12. }
  13. }
  14. return -1;
  15. }
  16. public static void main(String[] args) {
  17. Solution solution = new Solution();
  18. int[] nums = {1, 2, 3, 4, 5, 6, 6};
  19. solution.findNotSingle(nums);
  20. }
  21. }

2.求和法(数值连续)76

3.取反法

把数值作为一个数组的索引,将该数组的值取反,只出现一次为负数,要是出现两次,就会进行两次取反,两次取反后的结果为正数

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注