# Majority element in a linked list

Given a linked list, find majority element. An element is called **Majority element** if it appears more than or equal to **n/2** times where n is total number of nodes in the linked list.

Examples:

Input : 1->2->3->4->5->1->1->1->NULL Output : 1Explanation1 occurs 4 times Input :10->23->11->9->54->NULL Output :NO majority element

**Method 1(simple)**

Run two loops starting from head and count frequency of each element iteratively. Print the element whose frequency is greater than or equal to n/2. In this approach time complexity will be **O(n*n)** where n is the number of nodes in the linked list.

## C++

`// C++ program to find majority element in ` `// a linked list ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `/* Link list node */` `struct` `Node { ` ` ` `int` `data; ` ` ` `struct` `Node* next; ` `}; ` ` ` `/* Function to get the nth node from the ` `last of a linked list*/` `int` `majority(` `struct` `Node* head) ` `{ ` ` ` `struct` `Node* p = head; ` ` ` ` ` `int` `total_count = 0, max_count = 0, res = -1; ` ` ` `while` `(p != NULL) { ` ` ` ` ` `// Count all occurrences of p->data ` ` ` `int` `count = 1; ` ` ` `struct` `Node* q = p->next; ` ` ` `while` `(q != NULL) { ` ` ` `if` `(p->data == q->data) ` ` ` `count++; ` ` ` `q = q->next; ` ` ` `} ` ` ` ` ` `// Update max_count and res if count ` ` ` `// is more than max_count ` ` ` `if` `(count > max_count) ` ` ` `{ ` ` ` `max_count = count; ` ` ` `res = p->data; ` ` ` `} ` ` ` ` ` `p = p->next; ` ` ` `total_count++; ` ` ` `} ` ` ` ` ` `if` `(max_count >= total_count/2) ` ` ` `return` `res; ` ` ` ` ` `// if we reach here it means no ` ` ` `// majority element is present. ` ` ` `// and we assume that all the ` ` ` `// element are positive ` ` ` `return` `-1; ` `} ` ` ` `void` `push(` `struct` `Node** head_ref, ` `int` `new_data) ` `{ ` ` ` `struct` `Node* new_node = ` ` ` `(` `struct` `Node*)` `malloc` `(` `sizeof` `(` `struct` `Node)); ` ` ` `new_node->data = new_data; ` ` ` `new_node->next = (*head_ref); ` ` ` `(*head_ref) = new_node; ` `} ` ` ` `/* Driver program to test above function*/` `int` `main() ` `{ ` ` ` `/* Start with the empty list */` ` ` `struct` `Node* head = NULL; ` ` ` ` ` `// create linked ` ` ` `push(&head, 1); ` ` ` `push(&head, 1); ` ` ` `push(&head, 1); ` ` ` `push(&head, 5); ` ` ` `push(&head, 4); ` ` ` `push(&head, 3); ` ` ` `push(&head, 2); ` ` ` `push(&head, 1); ` ` ` ` ` `int` `res = majority(head); ` ` ` ` ` `if` `(res != (-1)) ` ` ` `cout << ` `"Majority element is "` `<< res; ` ` ` `else` ` ` `cout << ` `"No majority element"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Time Complexity O(n*n)

**Method 2** Use hashing technique. We count frequency of each element and then we print the element whose frequency is ≥ n/2;

## C++

`// CPP program to find majority element ` `// in the linked list using hashing ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `/* Link list node */` `struct` `Node { ` ` ` `int` `data; ` ` ` `struct` `Node* next; ` `}; ` ` ` `/* Function to get the nth node from the last ` ` ` `of a linked list*/` `int` `majority(` `struct` `Node* head) ` `{ ` ` ` `struct` `Node* p = head; ` ` ` ` ` `// Storing elements and their frequencies ` ` ` `// in a hash table. ` ` ` `unordered_map<` `int` `, ` `int` `> hash; ` ` ` ` ` `int` `total_count = 0; ` ` ` `while` `(p != NULL) { ` ` ` ` ` `// increase every element ` ` ` `// frequency by 1 ` ` ` `hash[p->data]++; ` ` ` ` ` `p = p->next; ` ` ` ` ` `total_count++; ` ` ` `} ` ` ` ` ` `// Check if frequency of any element ` ` ` `// is more than or equal to total_count/2 ` ` ` `for` `(` `auto` `x : hash) ` ` ` `if` `(x.second >= total_count/2) ` ` ` `return` `x.first; ` ` ` ` ` `// If we reach here means no majority element ` ` ` `// is present. We assume that all the element ` ` ` `// are positive ` ` ` `return` `-1; ` `} ` ` ` `void` `push(` `struct` `Node** head_ref, ` `int` `new_data) ` `{ ` ` ` `struct` `Node* new_node = ` ` ` `(` `struct` `Node*)` `malloc` `(` `sizeof` `(` `struct` `Node)); ` ` ` `new_node->data = new_data; ` ` ` `new_node->next = (*head_ref); ` ` ` `(*head_ref) = new_node; ` `} ` ` ` `// Driver program to test above function ` `int` `main() ` `{ ` ` ` ` ` `/* Start with the empty list */` ` ` `struct` `Node* head = NULL; ` ` ` `push(&head, 1); ` ` ` `push(&head, 1); ` ` ` `push(&head, 1); ` ` ` `push(&head, 5); ` ` ` `push(&head, 4); ` ` ` `push(&head, 3); ` ` ` `push(&head, 2); ` ` ` `push(&head, 1); ` ` ` ` ` `int` `res = majority(head); ` ` ` ` ` `if` `(res != (-1)) ` ` ` `cout << ` `"majority element is "` `<< res; ` ` ` `else` ` ` `cout << ` `"NO majority elemenet"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Time Complexity O(n)

majority element is 1

## Recommended Posts:

- Create new linked list from two given linked list with greater element at each node
- Create a linked list from two linked lists by choosing max element at each position
- Majority Element | Set-2 (Hashing)
- Second Smallest Element in a Linked List
- Move first element to end of a given Linked List
- Count majority element in a matrix
- Majority element in a circular array of 0's and 1's
- Find the Second Largest Element in a Linked List
- Find the first duplicate element in the linked list
- Find a peak element in Linked List
- 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)
- Maximum and Minimum element of a linked list which is divisible by a given number k

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.