Тёмный

Java program to find majority element in an array? 

Learn With KrishnaSandeep
Подписаться 172 тыс.
Просмотров 22 тыс.
50% 1

Опубликовано:

 

11 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 16   
@elpsycongree1132
@elpsycongree1132 Год назад
This solution will only work if the majority element occurs more than half the size of the input array. Otherwise, your choice of using a map was great.
@mayurkhare9805
@mayurkhare9805 Год назад
I think the logic is wrong, what if we have 2-3, 3-4,4-2,5-1 ?
@maheshetikala7667
@maheshetikala7667 Год назад
We will add one more else and print like "there is no majarity elements"
@kadamjava22
@kadamjava22 11 месяцев назад
First think before you concluded statement.
@sunilkumarkumar-rv1dt
@sunilkumarkumar-rv1dt Год назад
do you have online class for kids
@saivarunmaddineni1173
@saivarunmaddineni1173 Год назад
My code: public class MajorityElement { public static void main(String args[]){ int[] arr = {3,1,2,3,3,4}; int ans = majority(arr); System.out.println(ans); } static int majority(int[] arr){ int count =0; for(int i=0;i
@shubhamkumar-gw4vb
@shubhamkumar-gw4vb Год назад
return arr[i]; ?
@vengateshm2122
@vengateshm2122 Год назад
Why size/2 can't we use Max count here?
@springhibernatetutes
@springhibernatetutes Год назад
No we cannot use
@kotla2546
@kotla2546 Год назад
Send me ur code bro
@sainath2983
@sainath2983 3 месяца назад
public class Program { static void Main(string[] args) { int[] nums = { 2, 3, 4, 5, 2, 3, 3, 3, 3, 5, 5, 5, 5, 5, 5, 5 }; Console.WriteLine(MajorityElement(nums)); } public static int MajorityElement(int[] nums) { int count = 0; Dictionary dict = new Dictionary(); //Logic for (int i = 0; i < nums.Length; i++) { if (dict.ContainsKey(nums[i])) { dict[nums[i]]++; } else { dict.Add(nums[i], 1); } } foreach (var x in dict) { if (x.Value > count) { count = x.Key; } } return count; } }
@vv-ox1cw
@vv-ox1cw Год назад
Hi
@sunilkumarkumar-rv1dt
@sunilkumarkumar-rv1dt Год назад
may i know your contact number for online class for 6th class kid
@g.madhusudhan5944
@g.madhusudhan5944 Год назад
hi bro Can I get your number to contact you I want learn Java coding bro