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 Жыл бұрын
I think the logic is wrong, what if we have 2-3, 3-4,4-2,5-1 ?
@maheshetikala7667 Жыл бұрын
We will add one more else and print like "there is no majarity elements"
@kadamjava22 Жыл бұрын
First think before you concluded statement.
@sunilkumarkumar-rv1dt Жыл бұрын
do you have online class for kids
@sainath29838 ай бұрын
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; } }
@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 Жыл бұрын
return arr[i]; ?
@vengateshm21222 жыл бұрын
Why size/2 can't we use Max count here?
@springhibernatetutes2 жыл бұрын
No we cannot use
@kotla2546 Жыл бұрын
Send me ur code bro
@vv-ox1cw2 жыл бұрын
Hi
@sunilkumarkumar-rv1dt Жыл бұрын
may i know your contact number for online class for 6th class kid
@g.madhusudhan5944 Жыл бұрын
hi bro Can I get your number to contact you I want learn Java coding bro