You can use below code which uses HashSet() in Java.
public static boolean hasPairWithSum2(int arr[], int sum){
HashSet<Integer>mySet=new HashSet<Integer>();
int len = arr.length;
for (int i = 0; i < len; i++){
if (mySet.contains(arr[i]))
return true;
mySet.add(sum - arr[i]);
}
return false;
}
1
solved What can I use instead of Set() in Java?