Majority element in a linked list
Last Updated :
09 Jan, 2023
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 : 1
Explanation 1 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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
int majority( struct Node* head)
{
struct Node* p = head;
int total_count = 0, max_count = 0, res = -1;
while (p != NULL) {
int count = 1;
struct Node* q = p->next;
while (q != NULL) {
if (p->data == q->data)
count++;
q = q->next;
}
if (count > max_count)
{
max_count = count;
res = p->data;
}
p = p->next;
total_count++;
}
if (max_count >= total_count/2)
return res;
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;
}
int main()
{
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 element" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static Node head;
static class Node
{
int data;
Node next;
};
static int majority(Node head)
{
Node p = head;
int total_count = 0 ,
max_count = 0 , res = - 1 ;
while (p != null )
{
int count = 1 ;
Node q = p.next;
while (q != null )
{
if (p.data == q.data)
count++;
q = q.next;
}
if (count > max_count)
{
max_count = count;
res = p.data;
}
p = p.next;
total_count++;
}
if (max_count >= total_count / 2 )
return res;
return - 1 ;
}
static void push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
head = head_ref;
}
public static void main(String[] args)
{
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 ))
System.out.println( "Majority element is " + res);
else
System.out.println( "No majority element" );
}
}
|
Python3
head = None
class Node :
def __init__( self ):
self .data = 0
self . next = None
def majority(head):
p = head
total_count = 0
max_count = 0
res = - 1
while (p ! = None ):
count = 1
q = p. next
while (q ! = None ):
if (p.data = = q.data):
count = count + 1
q = q. next
if (count > max_count):
max_count = count
res = p.data
p = p. next
total_count = total_count + 1
if (max_count > = total_count / 2 ):
return res
return - 1
def push( head_ref, new_data):
global head
new_node = Node()
new_node.data = new_data
new_node. next = head_ref
head_ref = new_node
head = head_ref
head = None
push(head, 1 )
push(head, 1 )
push(head, 1 )
push(head, 5 )
push(head, 4 )
push(head, 3 )
push(head, 2 )
push(head, 1 )
res = majority(head)
if (res ! = ( - 1 )):
print ( "Majority element is " , res)
else :
print ( "No majority element" )
|
C#
using System;
class GFG
{
static Node head;
public class Node
{
public int data;
public Node next;
};
static int majority(Node head)
{
Node p = head;
int total_count = 0,
max_count = 0, res = -1;
while (p != null )
{
int count = 1;
Node q = p.next;
while (q != null )
{
if (p.data == q.data)
count++;
q = q.next;
}
if (count > max_count)
{
max_count = count;
res = p.data;
}
p = p.next;
total_count++;
}
if (max_count >= total_count / 2)
return res;
return -1;
}
static void push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
head = head_ref;
}
public static void Main(String[] args)
{
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))
Console.WriteLine( "Majority element is " + res);
else
Console.WriteLine( "No majority element" );
}
}
|
Javascript
<script>
var head = null ;
class Node
{
constructor()
{
this .data = 0;
this .next = null ;
}
};
function majority(head)
{
var p = head;
var total_count = 0,
max_count = 0, res = -1;
while (p != null )
{
var count = 1;
var q = p.next;
while (q != null )
{
if (p.data == q.data)
count++;
q = q.next;
}
if (count > max_count)
{
max_count = count;
res = p.data;
}
p = p.next;
total_count++;
}
if (max_count >= total_count / 2)
return res;
return -1;
}
function push(head_ref, new_data)
{
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
head = head_ref;
}
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);
var res = majority(head);
if (res != (-1))
document.write( "Majority element is " + res);
else
document.write( "No majority element" );
</script>
|
Output
Majority element is 1
Time Complexity: O(n*n)
Auxiliary Space: O(1)
Method 2: Use hashing technique. We count frequency of each element and then we print the element whose frequency is ? n/2;
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
int majority( struct Node* head)
{
struct Node* p = head;
unordered_map< int , int > hash;
int total_count = 0;
while (p != NULL) {
hash[p->data]++;
p = p->next;
total_count++;
}
for ( auto x : hash)
if (x.second >= total_count/2)
return x.first;
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;
}
int main()
{
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 element" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node next;
};
static int majority(Node head)
{
Node p = head;
HashMap<Integer,Integer> hash = new HashMap<Integer,Integer>();
int total_count = 0 ;
while (p != null )
{
if (hash.containsKey(p.data))
hash.put(p.data, hash.get(p.data) + 1 );
else
hash.put(p.data, 1 );
p = p.next;
total_count++;
}
for (Map.Entry<Integer,Integer> x : hash.entrySet())
if (x.getValue() >= total_count/ 2 )
return x.getKey();
return - 1 ;
}
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
public static void main(String[] args)
{
Node head = null ;
head = push(head, 1 );
head = push(head, 1 );
head = push(head, 1 );
head = push(head, 5 );
head = push(head, 4 );
head = push(head, 3 );
head = push(head, 2 );
head = push(head, 1 );
int res = majority(head);
if (res != (- 1 ))
System.out.print( "majority element is " + res);
else
System.out.print( "NO majority element" );
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def majority(head):
p = head;
hash = dict ()
total_count = 0 ;
while (p ! = None ):
if p.data not in hash :
hash [p.data] = 0
hash [p.data] + = 1
p = p. next ;
total_count + = 1
for x in hash .keys():
if ( hash [x] > = total_count / / 2 ):
return x;
return - 1 ;
def push(head_ref, new_data):
new_node = Node(new_data)
new_node. next = (head_ref);
(head_ref) = new_node;
return head_ref
if __name__ = = '__main__' :
head = None
head = push(head, 1 );
head = push(head, 1 );
head = push(head, 1 );
head = push(head, 5 );
head = push(head, 4 );
head = push(head, 3 );
head = push(head, 2 );
head = push(head, 1 );
res = majority(head);
if (res ! = ( - 1 )):
print ( "majority element is " + str (res))
else :
print ( "NO majority element" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static int majority(Node head)
{
Node p = head;
Dictionary< int ,
int > hash = new Dictionary< int ,
int >();
int total_count = 0;
while (p != null )
{
if (hash.ContainsKey(p.data))
hash[p.data] = hash[p.data] + 1;
else
hash.Add(p.data, 1);
p = p.next;
total_count++;
}
foreach (KeyValuePair< int , int > x in hash)
if (x.Value >= total_count/2)
return x.Key;
return -1;
}
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 1);
head = push(head, 1);
head = push(head, 1);
head = push(head, 5);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 1);
int res = majority(head);
if (res != (-1))
Console.Write( "majority element is " + res);
else
Console.Write( "NO majority element" );
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function majority(head)
{
var p = head;
var hash = {};
var total_count = 0;
while (p != null )
{
if (hash.hasOwnProperty(p.data)) hash[p.data] = hash[p.data] + 1;
else hash[p.data] = 1;
p = p.next;
total_count++;
}
for (const [key, value] of Object.entries(hash)) {
if (value >= parseInt(total_count / 2)) return key;
return -1;
}
}
function push(head_ref, new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
var head = null ;
head = push(head, 1);
head = push(head, 1);
head = push(head, 1);
head = push(head, 5);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 1);
var res = majority(head);
if (res != -1) document.write( "majority element is " + res);
else document.write( "NO majority element" );
</script>
|
Output
majority element is 1
Time Complexity: O(n)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...