Thanksgiving Sale: Use Coupon Code THANKS25 to Get Extra 25% Off.
00DAYS
:
00HOURS
:
00MINUTES
:
00SECONDS
Leetcode Problem 2229. Check if an Array Is Consecutive
2229. Check if an Array Is Consecutive
Leetcode Solutions
Check Consecutive Sequence Using Set and Range
Convert the input array nums to a set to remove any duplicate values.
Find the minimum value min_val in the set.
Find the maximum value max_val in the set.
Calculate the expected length of the consecutive sequence as max_val - min_val + 1.
Compare the expected length with the actual length of the array. If they are equal, and the size of the set is also equal to the length of the array, return true.
If the expected length does not match the actual length or the set size is not equal to the array length, return false.