The duplicate case is where people mess up.
Use a hash map to store the frequency of each element, then iterate through the unique elements and for each element x, check if target - x exists in the map. To avoid double-counting, only count each pair once by ensuring x <= target - x, and handle the case where x == target - x by checking if the frequency is at least 2.
Pro tip: Clarify whether the array can contain duplicates and whether the pairs are based on values or indices. Mention that the solution runs in O(n) time and O(n) space, which is optimal for this problem.
Restate the problem in your own words to ensure you understand that we need distinct unordered pairs of values from different indices, and that a value can pair with itself only if it appears at least twice.
Decide to use a hash map to count the frequency of each element, which allows O(1) lookups for the complement.
Iterate through the unique elements. For each element x, compute complement = target - x. If x < complement and complement exists in the map, add the product of their frequencies to the count. If x == complement and frequency[x] >= 2, add frequency[x] choose 2 to the count.
Consider cases like empty array, no pairs, all elements same, and target such that complement equals x. Ensure the logic correctly counts pairs without double-counting.
State that the time complexity is O(n) because we traverse the array once to build the frequency map and once through unique elements, and space complexity is O(n) for the map.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.