Skip to main content

Two-Sum Pattern

Learn Two-Sum Pattern (Hashing, Data Structures & Algorithms) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

The Two-Sum Pattern Find two numbers in an array that add up to a target. The brute force checks every pair - O(n²). A HashMap turns this into a single O(n) pass: for each number, check whether its COMPLEMENT (target - number) has already been seen.

More lessons in Hashing