Find the odd number of times in an array where the number of other numbers appear even several times in an array where the number of odd occurrences

Source: Internet
Author: User

Find the number of odd times in an array with an even number of other numbers the public
class oddnum{
	
	//(1) array has only one number occurrences of the odd number of times public
	static void Get01 (Int[]arr)
	{
         int cur=0;
         for (int i=0;i<arr.length;i++)
         {
         	cur=cur^arr[i];
         }
         SYSTEM.OUT.PRINTLN (cur);

	}

	(2) The odd number of times public
	static void Get02 (Int[]arr)
	{
         int cur=0,oddone=0 is
         present in two numbers in the array; for (int i=0;i<arr.length;i++)
         {
         	cur^=arr[i];
         }
         int rightone=cur& (~cur+1);
         for (int i=0;i<arr.length;i++)
         {
         	if ((rightone&arr[i))!=0)
         	{
         		oddone^=arr[i];
         	}
         System.out.println (oddone+ "" + (Cur^oddone));
	}
	public static void Main (String[]args)
	{
		int[]arr={1,2,2,3,3,5,5};
		int[]arr2={1,3,3,3,5,5};
		GET01 (arr);
		Get02 (ARR2);


	}


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.