Java use HashMap


  • 0
    G

    my code,use hashmap.

    public class Solution {
        public int singleNumber(int[] nums) {
        Map<Integer,Integer> map=new HashMap<Integer,Integer>();
        for(int num:nums){
        if(map.containsKey(num)){
            map.put(num,2);
        }else map.put(num,1);
        }
        for(Integer key:map.keySet()){
            if(map.get(key)==1){
                return key;
            }
        }
        return 0;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.