My lame hash table solution


  • 0
    S

    Runs in n time but uses space...

    public class Solution {
        public int singleNumber(int[] nums) {
            Map<Integer, Integer> hits = new HashMap<>();
            
            for (int i = 0; i < nums.length; i++) {
                Integer count = hits.get(nums[i]);
                
                if (count != null) {
                    hits.put(nums[i], count + 1);
                } else {
                    hits.put(nums[i], 1);
                }
            }
            
            for(Map.Entry<Integer, Integer> entry : hits.entrySet()) {
                if (entry.getValue() == 1) {
                    return entry.getKey();
                }
            }
            
            return -1;
        }
    }
    

  • 0
    S

    @Sovietaced without using extra memory, you utilize Map which is O(n) memory space.


Log in to reply
 

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