在一个数组里面,除了一个数是只有一个之外,其他都是有两个的,编写一个方法找出这唯一的一个数

 1 public class singleNum {
 2     /*
 3      * 因为a^b^a=b,所以1^2^2^3^3=1
 4      * 
 5      * */
 6     
 7     public int single(int A[])
 8     {
 9         int aim,i;
10         for(aim=0,i=0;i<A.length;i++)
11         {
12             aim=aim^A[i];
13         }
14         return aim;
15     }
16     
17 public static void main(String[] args) 
18     {
19     
20         int A[]={1,2,3,3,2};
21         singleNum sn=new singleNum();
22         int num=sn.single(A);
23         System.out.println(num);
24     
25     }
26 }

 

posted on 2014-09-06 12:27  daocaorendeshijie  阅读(173)  评论(0编辑  收藏  举报

导航