Find Majority Element ( LeetCode #169 ) Part-2 | FREE DSA Course in JAVA | Lecture 77

preview_player
Показать описание
We are up with a more efficient approach to solving the majority element question.

Here will see a more efficient approach where we reduce the time complexity of the program and hence write a faster program.

We need so because many times companies especially large product-based companies like Google, Amazon, and Meta not only look for an errorless solution but also the approach of the programmer.

Hence we need to take care of the time complexity and efficient approach.

Do check this approach and let us know in the comment if you were able to think so or not.

Subscribe @TAPACADEMY

or call us at 8884881203

#java #dsa #programming #arrayjava #array #coding #coder
Рекомендации по теме
Комментарии
Автор

thank you so much sir....ur explanation needs a honor....by watching your fantastical explanation for first 5mins....i coded the solution...once again thanks sir

Ben-xdgy
Автор

Thank you sir....for such a beautifully explained optimal solution! Your content is really commendable....! Thanks a ton for the whole superb playlist, it's really helpful to me....!!

aparajitashambhavee
Автор

Great way of explanation, I am beginner and for me this explanation with slow place and multiple time. Make me understand in more better way than other videos where they just take pen and paper and just walk through the program

dhru
Автор

Great Explanation !! Thank You !! From this Explanation very helpful for me!!

kdsolutionmaster
Автор

what if the array is 6, 6, 3, 3, 4, 3, 2

harshithanunnaguppala
Автор

this is the code if no majority element:

int maj=arr[0];
int coun=1;
for(int i=0;i<arr.length;i++){
if(arr[i]==maj){
coun++;
}
else{
coun--;
}
if(coun==0){
maj=arr[i];
coun++;
}
}
int fre=0;
int ans=0;
for(int i=0;i<arr.length;i++){
if(maj==arr[i]){
fre++;
}

}
if(fre>arr.length/2){
ans=maj;
}
else{
ans=-1;
}
return ans;

danishansari
Автор

Can this be a solution when there is majority element.
1.sort an array (0(logn))
2.int i=1
While(i<a.length) {
If(a[i]==a[i-1])
Count++;
i++;
else
If(count>n/2)
Break;
else
Count=0;
i++;
}
Return i-1;

saurabhchamoli