easyArraysPattern: Linear Scan

First Duplicate Node Solution

Problem Statement

You are given an array of integers `nodeIds` of size `n`, containing unique identifiers for surveillance nodes. The task is to perform a linear scan of the array and identify the first duplicate node ID found, which represents the breached node.

Examples

Example 1:
Input:[1, 2, 3, 4, 5, 2]
Output:2
Explanation: The first duplicate found in the array is 2.
Example 2:
Input:[1, 2, 3, 4, 5, 6, 7, 8, 9, 1]
Output:1
Explanation: The first duplicate found in the array is 1.

Constraints

  • 1 <= n <= 10^5
  • array elements are 0 or 1.
Time: O(N) Space: O(1)
The optimized approach involves using a linear scan with a time complexity of O(n) to find the breached node.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.