Topic
在其他数都出现偶数次的数组中找到出现奇数次的数
Java code
Package com.lizhouwei.chapter7;/** * @Description: Find the number of odd occurrences in an array where the other number appears even several times * @Author: Lizhouwei * @CreateDate: 2018/4/28 21:02 * @Modify by: * @ModifyDate: */public class Chapter7_5 {//finds the number of odd occurrences in an array where the other number appears even several times public int getonce (int[] ar R) {int once = 0; for (int i = 0; i < arr.length; i++) {once = once ^ arr[i]; } return once; }//In an array where the other number appears even several times, the number that appears odd number public int gettwo (int[] arr) {int once = 0; for (int i = 0; i < arr.length; i++) {once = once ^ arr[i]; } int rightone = once & (~once + 1);//Take the leftmost first occurrence 1 of the number of digits int one = once; for (int i = 0; i < arr.length; i++) {if ((Rightone & Arr[i])! = 0) {one = one ^ arr[i] ; }} System.out.print ("First number:" + one); System.out.println ("; second number:" + (once ^ one)); return once; }//test public static void main (string[] args) {chapter7_5 chapter = new ChApter7_5 (); Int[] arr = {1, 1, 2, 3, 3, 4, 4, 5, 5}; int reslt = chapter.getonce (arr); System.out.print ("{1,1,2,3,3,4,4,5,5}"); System.out.println ("Only one occurrence of the number is:" + reslt); Int[] arr1 = {1, 1, 2, 3, 3, 4, 5, 5}; System.out.print ("{1,1,2,3,3,4,5,5}"); Chapter.gettwo (ARR1); }}
Results
Programmer Code Interview guide Seventh bit arithmetic finds the number of odd occurrences in an array where the other numbers appear even several times