Programmer Code Interview guide Seventh bit arithmetic finds the number of odd occurrences in an array where the other numbers appear even several times

Source: Internet
Author: User
Tags arithmetic

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.