在一个整型数组中,除了两个数,其他的数都出现了2次,找出这个两个只出现了一次的数字

 1 public class Question5 {
 2     public static void main(String[] args) {
 3         //声明一个满足条件整数型数组(6和0一次)
 4         int[] a = {1, 2, 3, 4, 5, 1, 6, 2, 7, 3, 8, 4, 9, 5, 0, 7, 8, 9};
 5         //输出整数型数组
 6         for (int i = 0; i < a.length; i++) {
 7             System.out.print(a[i]);
 8         }
 9         System.out.println("\n只出现一次的字符为:");
10         //声明一个counter统计出现的次数
11         int counter;
12         //第一重遍历字符串
13         for (int i = 0; i < a.length; i++) {
14             counter = 0;   //计数器复位
15             // 第二层比较是否存在相同,从0开始遍历
16             for (int j = 0; j < a.length; j++) {
17                 //判断是否后面是否有相同字符,有就标记
18 
19                 if (a[i] == a[j]) {
20 //                    System.out.printf("%d,%d\n", a[i], a[j]);
21                     counter++;
22                 }
23             }
24             //如果计数器为1,则输出结果
25             if (counter == 1) {
26                 System.out.print(a[i] + " ");
27             }
28         }
29     }
30 }

猜你喜欢

转载自www.cnblogs.com/scwyqin/p/12411663.html
今日推荐