Intermediate Level
Implement the hasPairWithExactDifference method that checks whether any two values in the array have the required absolute difference.
The input contains an integer array nums, its length size, and an integer diff. Your task is to check whether any two values in the array have an exact absolute difference equal to diff.
Return true if such a pair exists; otherwise return false. For example, in [5,1,9,3] with diff = 4, the pair 5 and 1 has difference 4.
Use a set to remember values that have already been seen.
For each current value, check whether value - diff or value + diff already exists in the set. If yes, a valid pair exists. Otherwise, store the current value and continue.
Pseudocode:
function hasPairWithExactDifference(nums, size, diff):
seen = empty set
for each value in nums:
if value - diff exists in seen:
return true
if value + diff exists in seen:
return true
add value to seen
return false