First Unique Package ID
At Delhivery, we manage a vast network of logistics operations, processing millions of packages daily. Accurate tracking and processing are critical. Sometimes, due to various system interactions or re-routing events, a package ID might appear multiple times in a manifest or scan log. To optimize our processing workflows and avoid redundant operations, we need to efficiently identify unique package IDs, specifically focusing on the first one encountered that truly represents a single, distinct entry.
Implement a function findFirstUniquePackageId that identifies and returns the first package ID from a given list that appears exactly once throughout the entire list. This helps in quickly pinpointing packages that require immediate and unique attention.
packageIds - An array of integers, where each integer represents a unique package tracking ID. The order of IDs in the array matters for determining the "first" unique one.packageIds array. If no such package ID exists (i.e., all package IDs appear more than once), the function should return -1.packageIds will never be empty.-1.Examples
Example 1
Input:
packageIds = [1, 2, 3, 2, 1]
Output:
3
Note:
Package IDs 1 and 2 appear twice. Package ID 3 appears once, and it is the first such unique ID in the array.
Example 2
Input:
packageIds = [10, 20, 10, 30, 20, 40]
Output:
30
Note:
Package IDs 10 and 20 appear twice. Package ID 30 appears once. Although 40 also appears once, 30 is encountered earlier in the array.
Example 3
Input:
packageIds = [5, 5, 5, 5]
Output:
-1
Note:
All package IDs in the array are duplicates; none appear exactly once.
Example 4
Input:
packageIds = [7]
Output:
7
Note:
With only one package ID, it is by definition unique.
Constraints
1 <= packageIds.length <= 10^51 <= packageIds[i] <= 10^9 (Package IDs are positive integers)packageIds will contain only integers.Loading editor...