unique id
list processing
algorithm
programming
data management

Finding the lowest unused unique id in a list

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Finding the lowest unused unique ID in a list is a common task in computer science and software development. This problem often arises in scenarios like assigning new user IDs in a database, managing inventory IDs, or any situation where a unique identifier needs to be assigned efficiently without gaps. The objective is to determine the smallest number that isn't already present in a given list of integers. This article will explore methods to solve this problem, providing both technical explanations and practical examples.

Problem Definition

Given a list of integers representing used IDs, the challenge is to find the smallest non-negative integer that is not present in this list. This task can vary in complexity depending on the size of the list and the range of IDs it contains.

Example

Given the list: `[0, 1, 3, 4, 6]`

The smallest unused ID is `2`.

Technical Explanation

To determine the lowest unused unique ID efficiently, we need an algorithm that minimizes time complexity while maintaining simplicity in implementation. We will discuss two primary approaches: the brute force method and a more efficient set-based method.

Brute Force Method

The brute force approach involves iterating through the list starting from `0` and checking each consecutive integer to find the first one that isn't present in the list.

Steps:

  1. Initialize a counter to `0`.
  2. Increment the counter in a loop.
  3. Check if the current counter value is in the list.
  4. If not, break the loop; this is the result.
  5. Otherwise, continue.

Python Implementation:

  • Time Complexity: O(n×m)O(n \times m), where nn is the maximum value in the list and mm is the length of the list.
  • Space Complexity: O(1)O(1), constant space is used regardless of input size.
  • Time Complexity: O(n)O(n), where nn is the number of elements in the list.
  • Space Complexity: O(n)O(n) for storing the set.
    • Sort the list and iterate through from the start, checking for the first missing number.
    • Time Complexity: O(nlogn)O(n \log n) due to sorting.
    • Use bit manipulation if the range of numbers is known to be small, allowing for a fixed-size memory footprint.
    • Space Complexity: Efficient for small ranges.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.