
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if array contains contiguous integers with duplicates allowed in Python
Suppose we have an array of numbers called nums, it may have duplicate elements. We have to check whether it is a set of contiguous numbers or not.
So, if the input is like nums = [6, 8, 8, 3, 3, 3, 5, 4, 4, 7], then the output will be true as the elements are 3, 4, 5, 6, 7, 8.
To solve this, we will follow these steps −
- sort the list nums
- for i in range 1 to size of nums - 1, do
- if nums[i] - nums[i-1] > 1, then
- return False
- if nums[i] - nums[i-1] > 1, then
- return True
Let us see the following implementation to get better understanding −
Example
def solve(nums): nums.sort() for i in range(1,len(nums)): if nums[i] - nums[i-1] > 1: return False return True nums = [6, 8, 8, 3, 3, 3, 5, 4, 4, 7] print(solve(nums))
Input
[6, 8, 8, 3, 3, 3, 5, 4, 4, 7]
Output
True
- Related Articles
- Find a Fixed Point in an array with duplicates allowed in C++
- Check if it is possible to sort an array with conditional swapping of adjacent allowed in Python
- Check if a string has all characters with same frequency with one variation allowed in Python
- Check if an array contains all elements of a given range in Python
- Print distinct sorted permutations with duplicates allowed in input in C++
- Check if object contains all keys in JavaScript array
- Check if list contains consecutive numbers in Python
- Check if the given array contains all the divisors of some integer in Python
- Python - Check if Pandas dataframe contains infinity
- Check for duplicates in an array in MongoDB?
- Check if list contains all unique elements in Python
- How to check if array contains three consecutive dates in java?
- How to check if an array contains integer values in JavaScript ?
- Count number of triplets with product equal to given number with duplicates allowed in C++
- Check if array can be sorted with one swap in Python

Advertisements