Java program to find majority element in an array?

  Рет қаралды 22,414

Learn With KrishnaSandeep

Learn With KrishnaSandeep

Күн бұрын

Пікірлер: 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 Жыл бұрын
First think before you concluded statement.
@sunilkumarkumar-rv1dt
@sunilkumarkumar-rv1dt Жыл бұрын
do you have online class for kids
@sainath2983
@sainath2983 8 ай бұрын
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
@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 2 жыл бұрын
Why size/2 can't we use Max count here?
@springhibernatetutes
@springhibernatetutes 2 жыл бұрын
No we cannot use
@kotla2546
@kotla2546 Жыл бұрын
Send me ur code bro
@vv-ox1cw
@vv-ox1cw 2 жыл бұрын
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
Java program to find missing numbers in an array?
11:21
Learn With KrishnaSandeep
Рет қаралды 18 М.
# 5 Different ways to sum all elements in an array in java8
9:43
Learn With KrishnaSandeep
Рет қаралды 13 М.
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 62 МЛН
Mom Hack for Cooking Solo with a Little One! 🍳👶
00:15
5-Minute Crafts HOUSE
Рет қаралды 23 МЛН
Enceinte et en Bazard: Les Chroniques du Nettoyage ! 🚽✨
00:21
Two More French
Рет қаралды 42 МЛН
Try this prank with your friends 😂 @karina-kola
00:18
Andrey Grechka
Рет қаралды 9 МЛН
Java Program for String compression | Amazon Coding Interview Questions
14:28
Learn With KrishnaSandeep
Рет қаралды 25 М.
How to delete an element from an array in java?
12:16
Learn With KrishnaSandeep
Рет қаралды 64 М.
Better Java Streams with Gatherers - JEP Cafe #23
1:13:32
Java snake game 🐍
43:30
Bro Code
Рет қаралды 1,5 МЛН
Java Program to Find the Second Highest Number in an Array
9:26
Programming Tutorials
Рет қаралды 83 М.
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 62 МЛН