
- 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
Program to find the number of unique integers in a sorted list in Python
Suppose we have a list of sorted numbers called nums we have to find the number of unique elements in the list.
So, if the input is like nums = [3, 3, 3, 4, 5, 7, 7], then the output will be 4, as The unique numbers are [3, 4, 5, 7]
To solve this, we will follow these steps −
- s:= a new set
- cnt:= 0
- for each i in nums, do
- if i is not in s, then
- insert i into s
- cnt := cnt + 1
- if i is not in s, then
- return cnt
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, nums): s=set() cnt=0 for i in nums: if i not in s: s.add(i) cnt += 1 return cnt ob = Solution() print(ob.solve([3, 3, 3, 4, 5, 7, 7]))
Input
[3, 3, 3, 4, 5, 7, 7]
Output
4
- Related Articles
- Program to find least number of unique integers after K removals using Python
- Program to find number of unique people from list of contact mail ids in Python
- Convert number to list of integers in Python
- Program to find total unique duration from a list of intervals in Python
- Program to find number of different integers in a string using Python
- Python program to Sort a List of Strings by the Number of Unique Characters
- Program to find squared elements list in sorted order in Python
- Program to merge two sorted list to form larger sorted list in Python
- Program to find the sum of the absolute differences of every pair in a sorted list in Python
- Program to find number of sublists we can partition so given list is sorted finally in python
- Python program to find the largest number in a list
- Python program to find the smallest number in a list
- Program to find split a string into the max number of unique substrings in Python
- Python program to find largest number in a list
- Assign value to unique number in list in Python

Advertisements