{"id":"864f0ff3-f802-46a8-b3c2-4ede845d2214","name":"Majority Element - I","description":"1. Give an array of size 'n'.\r\n2. Find Majority element and print it(if exist), otherwise print \"No Majority Element exist\".\r\n3. Majority element-> if frequency of an element is more than n/2, then that element is majority element.\r\n3. Note : solve the problem in linear time and in O(1) space.\r\n","inputFormat":"Array = [2,2,1,1,1,2,2]\r\nArray = [1, 1, 5, 5, 3, 6]\r\n","outputFormat":"2\r\nNo Majority Element exist","constraints":"1. 1 &lt;= n &lt;= 10^4\r\n2. -10^9 &lt; arr[n] &lt; 10^9\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid printMajorityElement(vector<int> &arr) {\n // write your code here\n}\n\nint main(){\n int n;\n cin>>n;\n \n vector<int> arr(n);\n \n for(int i = 0; i < n; i++) {\n cin>>arr[i];\n }\n\n printMajorityElement(arr);\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~~~~~~~~~~~User Section~~~~~~~~~~~~~~~~~~~~~\r\n\r\n public static void printMajorityElement(int[] arr) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~~~~~~~~~~~~Input Management~~~~~~~~~~~~~~~~~~~\r\n\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n\r\n int[] arr = new int[n];\r\n\r\n for(int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n\r\n printMajorityElement(arr);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"7\r\n2 2 1 1 1 2 2","sampleOutput":"2","questionVideo":"https://www.youtube.com/embed/3tbjwaGC-ng","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"e6aff0dc-de30-4d60-8c11-f096e266ce43","name":"Majority Element - I","slug":"majority-element-i","type":1}],"next":{"id":"4c49c315-c851-41c8-a719-587e58bd1b5d","name":"Majority Element - I","type":3,"slug":"majority-element-i-7067"},"prev":{"id":"a3dd7f40-a570-48e2-a0c9-8486053772a1","name":"Container With Most Water MCQ","type":0,"slug":"container-with-most-water-mcq"}}}

# Majority Element - I

1. Give an array of size 'n'. 2. Find Majority element and print it(if exist), otherwise print "No Majority Element exist". 3. Majority element-> if frequency of an element is more than n/2, then that element is majority element. 3. Note : solve the problem in linear time and in O(1) space.

{"id":"864f0ff3-f802-46a8-b3c2-4ede845d2214","name":"Majority Element - I","description":"1. Give an array of size 'n'.\r\n2. Find Majority element and print it(if exist), otherwise print \"No Majority Element exist\".\r\n3. Majority element-> if frequency of an element is more than n/2, then that element is majority element.\r\n3. Note : solve the problem in linear time and in O(1) space.\r\n","inputFormat":"Array = [2,2,1,1,1,2,2]\r\nArray = [1, 1, 5, 5, 3, 6]\r\n","outputFormat":"2\r\nNo Majority Element exist","constraints":"1. 1 &lt;= n &lt;= 10^4\r\n2. -10^9 &lt; arr[n] &lt; 10^9\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid printMajorityElement(vector<int> &arr) {\n // write your code here\n}\n\nint main(){\n int n;\n cin>>n;\n \n vector<int> arr(n);\n \n for(int i = 0; i < n; i++) {\n cin>>arr[i];\n }\n\n printMajorityElement(arr);\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~~~~~~~~~~~User Section~~~~~~~~~~~~~~~~~~~~~\r\n\r\n public static void printMajorityElement(int[] arr) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~~~~~~~~~~~~Input Management~~~~~~~~~~~~~~~~~~~\r\n\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n\r\n int[] arr = new int[n];\r\n\r\n for(int i = 0; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n\r\n printMajorityElement(arr);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"7\r\n2 2 1 1 1 2 2","sampleOutput":"2","questionVideo":"https://www.youtube.com/embed/3tbjwaGC-ng","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"e6aff0dc-de30-4d60-8c11-f096e266ce43","name":"Majority Element - I","slug":"majority-element-i","type":1}],"next":{"id":"4c49c315-c851-41c8-a719-587e58bd1b5d","name":"Majority Element - I","type":3,"slug":"majority-element-i-7067"},"prev":{"id":"a3dd7f40-a570-48e2-a0c9-8486053772a1","name":"Container With Most Water MCQ","type":0,"slug":"container-with-most-water-mcq"}}}

Editor

# Majority Element - I

easy

1. Give an array of size 'n'. 2. Find Majority element and print it(if exist), otherwise print "No Majority Element exist". 3. Majority element-> if frequency of an element is more than n/2, then that element is majority element. 3. Note : solve the problem in linear time and in O(1) space.

## Constraints

1. 1 <= n <= 10^4 2. -10^9 < arr[n] < 10^9

## Format

### Input

Array = [2,2,1,1,1,2,2] Array = [1, 1, 5, 5, 3, 6]

### Output

2 No Majority Element exist

## Example

Sample Input

7 2 2 1 1 1 2 2

### Sample Output

2

Question Video

Discussions

Show Discussion

Related Resources