class Solution { public List> threeSum(int[] nums) { Set> result = new HashSet<>(); Set duplicates = new HashSet<>(); Map map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (duplicates.add(nums[i])) { for (int j = i + 1; j < nums.length; j++) { int target = -nums[i] - nums[j]; if (map.containsKey(target) && map.get(target) == i) { List temp = Arrays.asList(nums[i], nums[j], target); Collections.sort(temp); result.add(temp); } map.put(nums[j], i); } } } return new ArrayList<>(result); } }