π 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 tha...

Source: DEV Community
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).