SlideShare a Scribd company logo
1 of 2
Download to read offline
Given an array nums of size n, return the majority element.
The majority element is the element that appears more than round(n/2) times. You may assume
that the majority element always exists in the array.
Use the code template main_find_major_element
main_find_major_element:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// implement the function here.
int findMajorityElement(vector< int > &v)
{
return 0;
}
int main()
{
int n;
cout << "Enter the size of the vector: ";
cin >> n;
vector< int > input;
int element;
cout << "Enter the elements of the vector: ";
for ( int i = 0; i < n; i++)
{ cin >> element;
input.push_back(element);
}
int result = findMajorityElement(input);
cout << "The Majority Element is: " << result << endl;
return 0;
}
Examples:
Input:
Output:
3
Input:
Output:
2

More Related Content

Similar to Given an array nums of size n- return the majority element- The majori.pdf

C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
Mohamed Ahmed
 
Computer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdfComputer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdf
HIMANSUKUMAR12
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
info114
 
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
arihantelehyb
 
I am trying to fill out a program where the method definitions will b.docx
I am trying  to fill out a program where the method definitions will b.docxI am trying  to fill out a program where the method definitions will b.docx
I am trying to fill out a program where the method definitions will b.docx
Phil4IDBrownh
 
I have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfI have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdf
shreeaadithyaacellso
 

Similar to Given an array nums of size n- return the majority element- The majori.pdf (20)

C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
 
Oops lab manual2
Oops lab manual2Oops lab manual2
Oops lab manual2
 
Computer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdfComputer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdf
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
C++ Secure Programming
C++ Secure ProgrammingC++ Secure Programming
C++ Secure Programming
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
 
Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++
 
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
I am trying to fill out a program where the method definitions will b.docx
I am trying  to fill out a program where the method definitions will b.docxI am trying  to fill out a program where the method definitions will b.docx
I am trying to fill out a program where the method definitions will b.docx
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
 
Pointers in c++ programming presentation
Pointers in c++ programming presentationPointers in c++ programming presentation
Pointers in c++ programming presentation
 
OOP program questions with answers
OOP program questions with answersOOP program questions with answers
OOP program questions with answers
 
lecture7.ppt
lecture7.pptlecture7.ppt
lecture7.ppt
 
C++ file
C++ fileC++ file
C++ file
 
C++ file
C++ fileC++ file
C++ file
 
I have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfI have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdf
 
Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptx
 
functions
functionsfunctions
functions
 
JVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's TricksJVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's Tricks
 

More from MattCu9Parrd

More from MattCu9Parrd (20)

How many different 10-letter words (real or imaginary) can be formed f.pdf
How many different 10-letter words (real or imaginary) can be formed f.pdfHow many different 10-letter words (real or imaginary) can be formed f.pdf
How many different 10-letter words (real or imaginary) can be formed f.pdf
 
how does cell signaling instruct cells to form the primary axes of an.pdf
how does cell signaling instruct cells to form the primary axes of an.pdfhow does cell signaling instruct cells to form the primary axes of an.pdf
how does cell signaling instruct cells to form the primary axes of an.pdf
 
How does abilify directly relate to the diagnosis MDD- How does abili.pdf
How does abilify directly relate to the diagnosis MDD-  How does abili.pdfHow does abilify directly relate to the diagnosis MDD-  How does abili.pdf
How does abilify directly relate to the diagnosis MDD- How does abili.pdf
 
how do how sulfate reducers produce a proton motive force-- a-sulfat.pdf
how do how sulfate reducers produce a proton motive force--   a-sulfat.pdfhow do how sulfate reducers produce a proton motive force--   a-sulfat.pdf
how do how sulfate reducers produce a proton motive force-- a-sulfat.pdf
 
How do i know an element or a compound passed through the cell membran.pdf
How do i know an element or a compound passed through the cell membran.pdfHow do i know an element or a compound passed through the cell membran.pdf
How do i know an element or a compound passed through the cell membran.pdf
 
how could I return a length of stack of strings stack name is history.pdf
how could I return a length of stack of strings  stack name is history.pdfhow could I return a length of stack of strings  stack name is history.pdf
how could I return a length of stack of strings stack name is history.pdf
 
How can ethics training support organizational development-How can DEI.pdf
How can ethics training support organizational development-How can DEI.pdfHow can ethics training support organizational development-How can DEI.pdf
How can ethics training support organizational development-How can DEI.pdf
 
How ASOS has used digital to develop its differentiated market positio.pdf
How ASOS has used digital to develop its differentiated market positio.pdfHow ASOS has used digital to develop its differentiated market positio.pdf
How ASOS has used digital to develop its differentiated market positio.pdf
 
How are helix-turn-helix DNA-binding domains different that those foun.pdf
How are helix-turn-helix DNA-binding domains different that those foun.pdfHow are helix-turn-helix DNA-binding domains different that those foun.pdf
How are helix-turn-helix DNA-binding domains different that those foun.pdf
 
Great Pumplins- When growing giant pumplins for competitions- growers.pdf
Great Pumplins- When growing giant pumplins for competitions- growers.pdfGreat Pumplins- When growing giant pumplins for competitions- growers.pdf
Great Pumplins- When growing giant pumplins for competitions- growers.pdf
 
Given an exponential distribution with -20- what is the probability th.pdf
Given an exponential distribution with -20- what is the probability th.pdfGiven an exponential distribution with -20- what is the probability th.pdf
Given an exponential distribution with -20- what is the probability th.pdf
 
Given a double vector myVector- an input integer numElements- and an i.pdf
Given a double vector myVector- an input integer numElements- and an i.pdfGiven a double vector myVector- an input integer numElements- and an i.pdf
Given a double vector myVector- an input integer numElements- and an i.pdf
 
Given $100-000 to invest- construct a value-weighted portfolio of the.pdf
Given $100-000 to invest- construct a value-weighted portfolio of the.pdfGiven $100-000 to invest- construct a value-weighted portfolio of the.pdf
Given $100-000 to invest- construct a value-weighted portfolio of the.pdf
 
Given $100-000 to invest- construct a value-weighted portfolio of the (1).pdf
Given $100-000 to invest- construct a value-weighted portfolio of the (1).pdfGiven $100-000 to invest- construct a value-weighted portfolio of the (1).pdf
Given $100-000 to invest- construct a value-weighted portfolio of the (1).pdf
 
Give personalized examples for each of the following forms of justice-.pdf
Give personalized examples for each of the following forms of justice-.pdfGive personalized examples for each of the following forms of justice-.pdf
Give personalized examples for each of the following forms of justice-.pdf
 
Homologous Structures (1-2) that are inherited from a common ancestor.pdf
Homologous Structures (1-2) that are inherited from a common ancestor.pdfHomologous Structures (1-2) that are inherited from a common ancestor.pdf
Homologous Structures (1-2) that are inherited from a common ancestor.pdf
 
holla holla 1) Why can the recombination rate never exceed 50-- a).pdf
holla holla    1) Why can the recombination rate never exceed 50-- a).pdfholla holla    1) Why can the recombination rate never exceed 50-- a).pdf
holla holla 1) Why can the recombination rate never exceed 50-- a).pdf
 
HITECH Act meaningful use criteria includes which of the following- Se.pdf
HITECH Act meaningful use criteria includes which of the following- Se.pdfHITECH Act meaningful use criteria includes which of the following- Se.pdf
HITECH Act meaningful use criteria includes which of the following- Se.pdf
 
Histology- Identify the following 3 slides and the parts indicated- a-.pdf
Histology- Identify the following 3 slides and the parts indicated- a-.pdfHistology- Identify the following 3 slides and the parts indicated- a-.pdf
Histology- Identify the following 3 slides and the parts indicated- a-.pdf
 
Hippo signaling is a conserved pathway that controls cell prolifcratio.pdf
Hippo signaling is a conserved pathway that controls cell prolifcratio.pdfHippo signaling is a conserved pathway that controls cell prolifcratio.pdf
Hippo signaling is a conserved pathway that controls cell prolifcratio.pdf
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 

Given an array nums of size n- return the majority element- The majori.pdf

  • 1. Given an array nums of size n, return the majority element. The majority element is the element that appears more than round(n/2) times. You may assume that the majority element always exists in the array. Use the code template main_find_major_element main_find_major_element: #include <iostream> #include <vector> #include <algorithm> using namespace std; // implement the function here. int findMajorityElement(vector< int > &v) { return 0; } int main() { int n; cout << "Enter the size of the vector: "; cin >> n; vector< int > input; int element; cout << "Enter the elements of the vector: "; for ( int i = 0; i < n; i++) { cin >> element; input.push_back(element);
  • 2. } int result = findMajorityElement(input); cout << "The Majority Element is: " << result << endl; return 0; } Examples: Input: Output: 3 Input: Output: 2