Given a linked list of size N consisting of a string as node value, the task is to find the majority string, having frequency greater than [N/3], in the linked list.
Note: It is guaranteed that there is only one majority string.
Input: head -> geeks -> geeks -> abcd -> game -> knight -> geeks -> harry.
The frequency of geeks string in link list is 3 which is greater than [7/3] i.e 2.
Input: head -> hot -> hot -> cold -> hot -> hot
The frequency of hot string in the link list is 4 which is greater than [5/3] i.e 1.
Store the frequency of every string in a Map. Traverse the map and look for the string whose frequency is ≥ N / 3.
Time Complexity: O(N)
Auxiliary Space: O(N)
The idea is based on Moore’s Voting algorithm.
Find two candidates and check if any of these two candidates is actually a majority element or not.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find the first duplicate element in the linked list
- Find a peak element in Linked List
- Find the Second Largest Element in a Linked List
- Create new linked list from two given linked list with greater element at each node
- Sum of all odd frequency nodes of the Linked List
- Count minimum frequency elements in a linked list
- Python | Element with largest frequency in list
- Create a linked list from two linked lists by choosing max element at each position
- Find the element having different frequency than other array elements
- Find element in array with frequency equal to sum of frequencies of other elements
- Next greater element in the Linked List
- Majority element in a linked list
- Second Smallest Element in a Linked List
- Move first element to end of a given Linked List
- Move last element to front of a given Linked List | Set 2
- Rearrange a linked list in to alternate first and last element
- Move all occurrences of an element to end in a linked list
- Move last element to front of a given Linked List
- Search an element in a Linked List (Iterative and Recursive)
- Probability of finding an element K in a Singly Linked List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.