Not a member of GistPad yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- // approach: XOR (a^a = 0)
- public int singleNumber(int[] nums) {
- int n = nums.length;
- for(int ele : nums)
- {
- map.put(ele , map.getOrDefault(ele, 0) + 1);
- }
- int ans = 0;
- {
- int key = entry.getKey();
- int value = entry.getValue();
- if(value == 1)
- {
- ans = key;
- break;
- }
- }
- return ans;
- }
- // approach: XOR (a^a = 0)
- public int singleNumber(int[] nums) {
- int n = nums.length;
- int xor = 0;
- for(int ele : nums)
- {
- xor = xor ^ ele;
- }
- return xor;
- }
- }
RAW Paste Data
Copied
