
- 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 all array elements are distinct in Python
Suppose we have a list of numbers called nums, we have to check whether all elements in nums are unique or not.
So, if the input is like nums = [2, 3, 6, 5, 1, 8], then the output will be True as all elements are unique.
To solve this, we will follow these steps −
- n := size of l
- s := a new set
- for i in range 0 to n, do
- insert l[i] into s
- return true when size of s is same as size of l, otherwise false
Let us see the following implementation to get better understanding −
Example
def solve(l) : n = len(l) s = set() for i in range(0, n): s.add(l[i]) return (len(s) == len(l)) l = [2, 3, 6, 5, 1, 8] print(solve(l))
Input
[2, 3, 6, 5, 1, 8]
Output
True
- Related Articles
- Check if all elements of the array are palindrome or not in Python
- Check if array elements are consecutive in Python
- Python - Check if all elements in a List are same
- Python - Check if all elements in a list are identical
- Check if all sub-numbers have distinct Digit product in Python
- Check if an array contains all elements of a given range in Python
- Check if list contains all unique elements in Python
- Python program to print all distinct elements of a given integer array.
- Count distinct elements in an array in Python
- Print All Distinct Elements of a given integer array in C++
- Check if Queue Elements are pairwise consecutive in Python
- Check if some elements of array are equal JavaScript
- Python – Check if elements index are equal for list elements
- JavaScript Checking if all the elements are same in an array
- Check if given array is almost sorted (elements are at-most one position away) in Python

Advertisements