I have came up with this code when I try to find indexes of unique pairs whose total equals to target number. Time complexity and Space complexity both are O(n) for this solution. Is there any other efficient way to solve it? or what would be follow up questions for this one ?
public static ArrayList<int[]> findingTwoSum(int arr[], int target){
if (arr == null || arr.length < 1){
throw new IllegalArgumentException();
}
HashMap<Integer, Integer> map = new HashMap<>();
ArrayList<int[]> result = new ArrayList<>();
for (int i = 0 ; i < arr.length ; i++){
if (!map.containsKey(arr[i])){
map.put(target-arr[i], i);
}
else{
if(!result.contains(map.get(arr[i]))){
result.add(new int[]{i,map.get(arr[i])});
}
}
}
return result;
}