Algorithms and Data Structures · Senior · Question 64 of 75

How do you find all pairs that sum to a target in an array with duplicates, without double counting?

Explanation

For each distinct x pair it with count[target - x]; when x equals its complement use c × (c - 1) / 2. Process each value pair once (x < complement). A Set loses the counts.

Further reading

Problem with a question

Algorithms and Data Structures · Senior · question 64

Test yourself

Take the Algorithms and Data Structures · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz