
- 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 check whether elements frequencies are even or not in Python
Suppose we have a list of elements called nums, we have to check whether all numbers appear even times or not. We have to solve it using constant space.
So, if the input is like nums = [8, 9, 9, 8, 5, 5], then the output will be True, because all number have occurred twice.
To solve this, we will follow these steps −
if size of nums is odd, then
return False
sort the list nums
for i in range 1 to size of nums, do
if nums[i] is same as nums[i - 1], then
nums[i] := 0,
nums[i - 1] := 0
return true when sum of all elements present in nums is same as 0 otherwise false
Example
Let us see the following implementation to get better understanding
def solve(nums): if len(nums) & 1: return False nums.sort() for i in range(1, len(nums)): if nums[i] == nums[i - 1]: nums[i] = nums[i - 1] = 0 return sum(nums) == 0 nums = [8, 9, 9, 8, 5, 5] print(solve(nums))
Input
[8, 9, 9, 8, 5, 5]
Output
True
- Related Articles
- Program to check whether parentheses are balanced or not in Python
- Program to check whether two sentences are similar or not in Python
- Check whether the frequencies of all the characters in a string are prime or not in Python
- Program to check whether two string arrays are equivalent or not in Python
- Program to check whether all leaves are at same level or not in Python
- Program to check whether domain and range are forming function or not in Python
- Program to check whether different brackets are balanced and well-formed or not in Python
- Program to check whether all palindromic substrings are of odd length or not in Python
- Program to check whether leaves sequences are same of two leaves or not in python
- C++ Program to Check Whether Number is Even or Odd
- Program to check whether given graph is bipartite or not in Python
- Program to check whether given password meets criteria or not in Python
- Python program to check whether a list is empty or not?
- Golang Program to Check Whether Two Matrices are Equal or Not
- Swift Program to Check Whether Two Matrices Are Equal or Not

Advertisements