Given an integer array of size n, find all elements that appear more than [n/3] times. If no such element exists, return -1.
Array_nums = list(map(int,input('Array : ').split(','))) N=len(Array_nums) L=[] for i in range(N): Occ=Array_nums.count(Array_nums[i]) if Occ > N/3 : L.append(Array_nums[i]) print(-1) if not L else print(list(set(L)))
Input_1:
Array : 20,30,10,10,5,4,20,1,2
Output:
-1
Input_2:
Array : 3,2,3
Output:
[3]
Input_3:
Array : 1
Output:
[1]
Input_4:
Array : 1,2
Output:
[1, 2]
Input_5:
Array : 10,10,20,30,10,10
Output:
[10]
Illustration of the executed Output
More Q
- Find the majority element appearing more than n/2 times
- Return an array of the squares of each number.
- Highest factor in addition.
- Find the empty packets(0) of chocolate and push it to the end of the conveyor belt(array).
- Every element in array appears twice.Find that single one.
- Find the duplicate letters in string. Same to do in dictionary way.
- Find minimum number of edits (operations) required to convert ‘str1’ into ‘str2’.
- Return the maximum number you can get by changing at most one digit.
- Sed Commands – Ranges of lines.
- Find the kth smallest element in the given 2D array.
- Find all elements that appear more than [n/3] times.
- Write a function to return true if s2 contains the permutation of s1.
- Sort the array into a wave like array.
- Re-order array, such that
nums[0] < nums[1] > nums[2] < nums[3]...
. - Given the coordinates, return true if the four points construct a square.
- Minimum Distance Between Words of a String.
- Return the shortest distance between these two words.
- Write a program to find the n-th ugly number.
- Check if array could become Non – Decreasing array.
- Find the minimum area of a rectangle that can be formed from given points.