-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion 29.py
More file actions
38 lines (31 loc) · 1.39 KB
/
Question 29.py
File metadata and controls
38 lines (31 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Concept name : Array
# Question : Majority Element - More Than n/3
# Given an array arr[] consisting of n integers, the task is to find all the array elements which occurs more than floor(n/3) times.
# Note: The returned array of majority elements should be sorted.
# Examples:
# Input: arr[] = [2, 2, 3, 1, 3, 2, 1, 1]
# Output: [1, 2]
# Explanation: The frequency of 1 and 2 is 3, which is more than floor n/3 (8/3 = 2).
# Solution :
class Solution:
def findMajority(self, arr):
n=len(arr)
freq={}
l=[]
for i in arr:
if i not in freq:
freq[i]=1
else:
freq[i]+=1
for keys,count in freq.items():
if count>n/3:
l.append(keys)
l.sort()
return l
# Explanation : Create an empty dictionary freq to store frequencies of each number.
# Traverse the array:
# If a number is not in the dictionary, initialize it with 1.
# Else, increment its count.
# After the count is complete, loop through freq.items():
# If the count of an element is greater than n // 3, add it to the result list l.
# Sort the result list l before returning.