找出数组中的单身狗;

1. OddOccurrencesInArray
Find value that occurs in odd number of elements. A non-empty zero-indexed array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired. For example, in array A such that: A[0] = 9 A[1] = 3 A[2] = 9
A[3] = 3 A[4] = 9 A[5] = 7
A[6] = 9
the elements at indexes 0 and 2 have value 9,
the elements at indexes 1 and 3 have value 3,
the elements at indexes 4 and 6 have value 9,
the element at index 5 has value 7 and is unpaired.
Write a function: class Solution { public int solution(int[] A); } that, given an array A consisting of N integers fulfilling the above conditions, returns the value of the unpaired element. For example, given array A such that: A[0] = 9 A[1] = 3 A[2] = 9
A[3] = 3 A[4] = 9 A[5] = 7
A[6] = 9
the function should return 7, as explained in the example above. Assume that: N is an odd integer within the range [1..1,000,000];
each element of array A is an integer within the range [1..1,000,000,000];
all but one of the values in A occur an even number of times.
Complexity: expected worst-case time complexity is O(N);
expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.
package com.code;

import java.util.Arrays;

public class Test02 {

    public int solution(int[] A) {
// write your code in Java SE 8
int size = A.length;
if(size%2==0){
return -1;
}
//System.out.println(Arrays.toString(A));
Arrays.sort(A);
//System.out.println(Arrays.toString(A));
if(size==1){
return A[0];
}
if(A[0]!=A[1]){
return A[0];
}
if(A[size-1] != A[size-2]){
return A[size-1];
}
for(int i=2;i<size-1;i=i+2){
if(A[i]!=A[i+1]){
return A[i];
}
}
return 0;
} public static void main(String[] args) {
Test02 t02 = new Test02();
int[] a = {9,3,9,3,5}; System.out.println(t02.solution(a));
int [] b = {1,1,2,2,3,4,4}; System.out.println(t02.solution(b)); }
}

1. 找出数组中的单身狗OddOccurrencesInArray Find value that occurs in odd number of elements.的更多相关文章

  1. [LeetCode] Find All Numbers Disappeared in an Array 找出数组中所有消失的数字

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...

  2. 剑指Offer:找出数组中出现次数超过一半的元素

    题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include <stdio.h> int ha ...

  3. 找出数组中出现奇数次的元素<异或的应用>

    点击打开链接:百度面试题之找出数组中之出现一次的两个数(异或的巧妙应用) 题目描述|:给定一个包含n个整数的数组a,其中只有一个整数出现奇数次,其他整数都出现偶数次,请找出这个整数 使用异或操作,因为 ...

  4. 找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数

    找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数 #include<iostream>using namespace s ...

  5. 找出数组中最大值and索引

    找出数组中的最大值和和最大值的索引位置..... 第一中方法: /** * 找出数组中最大值和最大值的索引 * @param args */ public static void main(Strin ...

  6. 剑指offer:1.找出数组中重复的数(java版)

    数组中重复的数:题目:找出数组中重复的数,题目描述:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任 ...

  7. 【Java】 剑指offer(1) 找出数组中重复的数字

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字 ...

  8. python找出数组中第二大的数

    #!usr/bin/env python #encoding:utf-8 ''''' __Author__:沂水寒城 功能:找出数组中第2大的数字 ''' def find_Second_large_ ...

  9. 剑指offer.找出数组中重复的数字

    题目: 给定一个长度为 n 的整数数组 nums,数组中所有的数字都在 0∼n−1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中任意一个重复的数 ...

随机推荐

  1. js作用域、异步——学习笔记

    所有的 for if switch while do 等等,都属于块级作用域,里面声明的对象,外面也能访问.但function 函数里的作用域,在函数外是访问不到的. 但函数作用域里面可以访问外面. ...

  2. SQLiteDeveloper 工具

    破解方法: cmd下执行命令:  reg delete HKEY_CURRENT_USER\SharpPlus\SqliteDev /f

  3. CF864D Make a Permutation!

    思路: 贪心,构造,模拟. 实现: #include <bits/stdc++.h> using namespace std; ], a[], vis[], n; int main() { ...

  4. FCC 基础JavaScript 练习6

    1.对象和数组很相似,数组是通过索引来访问和修改数据,对象是通过属性来访问和修改数据的, 对象适合用来存储结构化数据,就和真实世界的对象一模一样,比如一只猫. 任务 创建一个叫做myDog的对象,它里 ...

  5. python自动化--语言基础二运算符、格式化输出、条件语句、循环语句、列表、元组

    运算符包括:算术运算符.比较运算符.赋值运算符.逻辑运算符.成员运算符.身份运算符. 算术运算符 %   取模(余数) //  取相除的整数部分 /   (5/2=2.5) 比较运算符 ==  等于 ...

  6. CAD使用GetxDataDouble读数据(网页版)

    主要用到函数说明: MxDrawEntity::GetxDataDouble2 读取一个Double扩展数据,详细说明如下: 参数 说明 [in] LONG lItem 该值所在位置 [out, re ...

  7. ThinkPHP---thinkphp拓展之空操作

    [一]概论 (1)定义 空操作指系统在找不到指定操作方法的时候.会定位到空操作方法 / 控制器来执行,利用这个机制,我们可以实现错误页面的自定义和URL的优化 (2)场景 常用于错误页面的自定义 (3 ...

  8. 08Microsoft SQL Server 数据查询

    Microsoft SQL Server 数据查询 单表查询所有列 --查询所有行所有列 select all * from table; --查询不重复行的所有列 select distinct * ...

  9. 通过Oracle函数SQL实现C# String.Format字符串格式化功能

    语言国际化要求,开发上要求Oracle数据库SQL中对应的返回信息-Message,实现一个通用函数调用,比如:提示信息内容:条码123456当前工站在FCT!”,即通用的信息内容格式化标准为:“条码 ...

  10. Python之IO编程

    前言:由于程序和运行数据是在内存中驻留的,由CPU这个超快的计算核心来执行.当涉及到数据交换的地方,通常是磁盘.网络等,就需要IO接口.由于CPU和内存的速度远远高于外设的速度,那么在IO编程中就存在 ...