DEV Community

Praven Kumar
Praven Kumar

Posted on

🔍 Finding Missing Numbers in an Array using Java Streams

Input: [1,2,1,2,5,8]
Output: [3,4,6,7]
🧠 Approach:
1️⃣ Find minimum and maximum values
2️⃣ Store elements in a Set for fast lookup
3️⃣ Generate range between min and max
4️⃣ Filter out numbers that are not present
❓ Why rangeClosed() and not range()?
IntStream.range(start, end) → excludes end value
IntStream.rangeClosed(start, end) → includes end value
👉 Example:
range(1, 5) → 1,2,3,4
rangeClosed(1, 5) → 1,2,3,4,5
💡 Since we want to check all numbers from min to max (inclusive),
we use rangeClosed(min, max).

Top comments (0)