Java Solution Using HashMap


  • -1
    R

    '''public class Solution {
    public int singleNonDuplicate(int[] nums) {
    HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
    for(int i = 0; i < nums.length; i++){
    hm.put(nums[i], 0);
    }
    for(int i : nums){
    if(hm.containsKey(i)){
    hm.put(i, hm.get(i) + 1);
    }
    }
    int t = 0;
    for(int i : hm.keySet()){
    if(hm.get(i) == 1){
    t = i;
    }
    }
    return t;
    }
    }
    '''


Log in to reply
 

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