
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Find Element Occurring Exactly Once in Python
Suppose we have a list of numbers called nums where each value occurs exactly three times except one value that occurs once. We have to find the unique value. We have to solve it in constant space.
So, if the input is like nums = [3, 3, 3, 8, 4, 4, 4], then the output will be 8
To solve this, we will follow these steps −
m := a map with different values and their frequencies
return the value with minimum frequency
Let us see the following implementation to get better understanding −
Example
from collections import Counter class Solution: def solve(self, nums): nums = Counter(nums) return min(nums, key=nums.get) ob = Solution() nums = [3, 3, 3, 8, 4, 4, 4] print(ob.solve(nums))
Input
[3, 3, 3, 8, 4, 4, 4]
Output
8
Advertisements