Check if array contains contiguous integers with duplicates allowed in Python

A sequence of numbers is said to be contiguous if the numbers can be arranged in such a way that each number follows the previous one without any gaps. For example, [11,12,13,14] is a contiguous sequence because there are no missing numbers between the smallest and largest values.

In this article, we need to check if an array contains such contiguous integers, even when duplicates are present. For instance, [11,12,13,13,14] should still be considered contiguous because the unique numbers are [11,12,13,14], which form a proper sequence without breaks.

Understanding the Problem

To check if an array contains contiguous integers with duplicates allowed, we need to:

  • Extract unique elements from the array
  • Find the range between minimum and maximum values
  • Compare the range length with the number of unique elements

Algorithm

The key insight is that if we have a contiguous sequence, the number of unique elements should equal the range from minimum to maximum value plus one.

def is_contiguous(arr):
    unique_elements = set(arr)
    min_val = min(unique_elements)
    max_val = max(unique_elements)
    return len(unique_elements) == (max_val - min_val + 1)

Example 1: Contiguous Array with Duplicates

Let's check if the array [11,12,13,13,14] contains contiguous integers ?

def is_contiguous(arr):
    unique_elements = set(arr)
    min_val = min(unique_elements)
    max_val = max(unique_elements)
    return len(unique_elements) == (max_val - min_val + 1)

arr = [11, 12, 13, 13, 14]
result = is_contiguous(arr)
print(f"Array {arr} is contiguous: {result}")

# Let's see the breakdown
unique = set(arr)
print(f"Unique elements: {sorted(unique)}")
print(f"Range: {min(unique)} to {max(unique)}")
print(f"Range length: {max(unique) - min(unique) + 1}")
print(f"Unique count: {len(unique)}")
Array [11, 12, 13, 13, 14] is contiguous: True
Unique elements: [11, 12, 13, 14]
Range: 11 to 14
Range length: 4
Unique count: 4

Example 2: Non-Contiguous Array

Let's check an array with gaps in the sequence ?

def is_contiguous(arr):
    unique_elements = set(arr)
    min_val = min(unique_elements)
    max_val = max(unique_elements)
    return len(unique_elements) == (max_val - min_val + 1)

arr = [2, 3, 5, 6, 2, 7]
result = is_contiguous(arr)
print(f"Array {arr} is contiguous: {result}")

# Let's see the breakdown
unique = set(arr)
print(f"Unique elements: {sorted(unique)}")
print(f"Range: {min(unique)} to {max(unique)}")
print(f"Range length: {max(unique) - min(unique) + 1}")
print(f"Unique count: {len(unique)}")
Array [2, 3, 5, 6, 2, 7] is contiguous: False
Unique elements: [2, 3, 5, 6, 7]
Range: 2 to 7
Range length: 6
Unique count: 5

Example 3: Single Element Array

A single element is always considered contiguous ?

def is_contiguous(arr):
    unique_elements = set(arr)
    min_val = min(unique_elements)
    max_val = max(unique_elements)
    return len(unique_elements) == (max_val - min_val + 1)

arr = [111]
result = is_contiguous(arr)
print(f"Array {arr} is contiguous: {result}")
Array [111] is contiguous: True

Alternative Approach Using Sorting

Another way to check contiguity is by sorting the unique elements and verifying consecutive differences ?

def is_contiguous_sorted(arr):
    unique_sorted = sorted(set(arr))
    for i in range(1, len(unique_sorted)):
        if unique_sorted[i] - unique_sorted[i-1] != 1:
            return False
    return True

# Test with different arrays
test_arrays = [
    [11, 12, 13, 13, 14],
    [2, 3, 5, 6, 2, 7],
    [1, 3, 2, 4, 3],
    [10]
]

for arr in test_arrays:
    result = is_contiguous_sorted(arr)
    print(f"Array {arr} is contiguous: {result}")
Array [11, 12, 13, 13, 14] is contiguous: True
Array [2, 3, 5, 6, 2, 7] is contiguous: False
Array [1, 3, 2, 4, 3] is contiguous: True
Array [10] is contiguous: True

Comparison of Methods

Method Time Complexity Space Complexity Best For
Range Comparison O(n) O(n) Simple and efficient
Sorting Approach O(n log n) O(n) Understanding the logic step by step

Conclusion

To check if an array contains contiguous integers with duplicates, convert to a set to remove duplicates, then verify if the range length equals the unique element count. The range comparison method is more efficient with O(n) time complexity.

Updated on: 2026-03-25T14:21:57+05:30

586 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements