找出只出现一次的数字

 1 public class test72 {
 2     /**
 3      * 找出只出现一次的数字
 4      * 使用计数器
 5      *
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         int arr1[] = {1, 1, 4, 3, 3, };
10         int num = fac1(arr1);
11         System.out.println(num);
12     }
13 
14     public static Integer fac1(int a[]) {
15 
16         for(int i = 0; i < a.length; i++)
17         {
18             int  count = 1;//出现第一次,计数1次
19             for(int j = 0; j < a.length; j++) {
20                 if(i == j) {
21                     continue;//下标相同,进入下一层循环
22                 }
23                 if(a[i] == a[j]) {
24                     count++;//下标不同,相同计数+1
25                 }
26             }
27             if(count == 1) {
28                 return a[i];
29             }
30         }
31         return null;//用Integer.而不是Int
32     }
33 }

先出现哪个数字且只出现一次,则打印该数字

posted @ 2020-04-26 20:08  听说在北郭  阅读(191)  评论(0)    收藏  举报