Skip to main content
Back to problems
#2229
Easy Algorithms

Check if an array is consecutive

Array Hash Table Sorting
62.2% acceptance
Mar 31, 2026
90
11
Given an integer array nums, return true if nums is consecutive, otherwise return false. An array is consecutive if it contains every number in the range [x, x + n - 1] (inclusive), where x is the minimum number in the array and n is the length of the array.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn is_consecutive(nums: Vec<i32>) -> bool {
    use std::collections::HashSet;
    let n = nums.len();
    let set: HashSet<i32> = nums.iter().copied().collect();
    if set.len() != n { return false; }
    let mn = *nums.iter().min().unwrap();
    let mx = *nums.iter().max().unwrap();
    (mx - mn + 1) as usize == n
  }
}