publicclassSolution { /** * @param nums: an array of integers * @return: the number of unique integers */publicintdeduplication(int[] nums) {Map<Integer,Boolean> map =newHashMap<>();for (int num : nums) {map.putIfAbsent(num,true); }int res =0;for (int key :map.keySet()) { nums[res++] = key; }return res; }}
Notes
Time complexity: O(n)
Space complexity: O(n)
Solution 2 (Java)
publicclassSolution { /** * @param nums: an array of integers * @return: the number of unique integers */publicintdeduplication(int[] nums) {if (nums ==null||nums.length==0)return0;Arrays.sort(nums);int len =0;for (int i =0; i <nums.length; i++) {if (nums[i] != nums[len]) { nums[++len] = nums[i]; } }return len +1; }}// i// [1,2,3,4,5,6,3,4,5,6]// l