数组中只出现一次的两个数字

 

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param array int整型一维数组 
     * @return int整型一维数组
     */
    public int[] FindNumsAppearOnce (int[] array) {
        // write code here
        //数组遍历,放入map中,value默认为1,如果有重复的数字,那么map的value加1
        //遍历map,找出value位1的k
        Map<Integer, Integer> map = new HashMap<Integer,Integer>();
        for(int i=0; i<array.length; i++){
            int tmp = array[i];
            if(!map.containsKey(tmp)){
                map.put(tmp,1);
            }else{
                map.put(tmp, map.get(tmp)+1);
            }
        }
        int[] re = new int[2];
        int j=0;
        for(Integer tmp: map.keySet()){
            if(map.get(tmp).equals(1)){
                re[j] = tmp;
                j++;
            }
        }
        return re;

    }
}

 

posted @ 2022-11-12 10:11  northli  阅读(10)  评论(0编辑  收藏  举报