


The first line contains a single integer ‘T’ denoting the number of test cases. The test cases follow.
The first line of each test case contains a single integer ‘N’ denoting the number of elements in the array.
The second line contains ‘N’ single space-separated integers denoting the elements of the array/list.
For each test case, return all the majority elements separated by a single space.
The output of every test case will be printed in a separate line.
You may return the majority of elements in any order.
You don’t need to print anything; It has already been taken care of. Just implement the given function.
1 <= T <= 100
3 <= N <= 5000
1 <= ARR[i] <= 10^5
Time Limit: 1 sec
The idea is to count the number of times each element occurs and if any element occurs more than N/3 times, then include it in our answer.
The steps are as follows:
The idea is to store the frequency of each element from the given array/list into the HashMap and then include all the elements with frequencies greater than N/3 in the answer.
Steps are as follows:
2.Iterate through the HashMap and store all the elements whose frequency is greater than N/3, where ‘N’ is the number of elements in the given array/list.
The idea is to sort the array and then count the number of occurrences of each element in a single traversal.
The steps are as follows:
The idea is to use a modification of Moore's voting algorithm to find candidate elements that may occur more than N/3 times in the given array. We will use the fact that if we remove 3 distinct elements from the array, the elements which occurred more than N/3 times do not change.
The steps are as follows: