Find a number that appears only once in a set of data

Ideas

  • Two-tier loop
  • The outer loop traverses the data sequentially from beginning to end
  • The inner loop finds in the data whether there is a number that is the same as itself in addition to itself
  • If found, then the conditions are not met, jump out of the inner loop, and continue to judge the next
  • If it cannot be found (that is, the inner loop is completed ), then it meets the conditions and print it out directly

Code

public class pra1220 {
    public static void main(String[] args) {
        int[] arr={9,5,9,2,5,7,7,7,2,6};
        String result=Arrays.toString(arr);
        System.out.print(result+"中只出现一次的数字是:");
        findNum(arr);
    }

    public static void findNum(int[] arr) {
        for(int cur=0;cur<arr.length;cur++){
            int find=0;
            for(;find<arr.length;find++){
                if(find!=cur&&arr[find]==arr[cur]){
                    break;
                }
            }
            //判断是不是内层循环循环完毕了也没有找到
            if(find==arr.length){
                System.out.println(arr[cur]);
                break;
            }
        }
    }
}

operation result

Guess you like

Origin blog.csdn.net/weixin_43939602/article/details/112174087
Recommended