

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Check if array elements are consecutive in Python
- Check if all elements of the array are palindrome or not 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 list contains all unique elements in Python
- Check if an array contains all elements of a given range in Python
- Check if some elements of array are equal JavaScript
- Check if Queue Elements are pairwise consecutive in Python
- Python – Check if elements index are equal for list elements
- Python program to print all distinct elements of a given integer array.
- JavaScript Checking if all the elements are same in an array
- Count distinct elements in an array in Python
- Print All Distinct Elements of a given integer array in C++
- Check if the elements of stack are pairwise sorted in Python
Advertisements