Most Frequent Element

preview_player
Показать описание
This teaches you how to determine what is the most frequent element in an array.
Рекомендации по теме
Комментарии
Автор

You're looping the entire array for each element. For efficiency purposes (time complexity), that's O(n^2) and it's expensive. A little more intermediate/advanced method is to use a Map/HashMap to store each element already examined, then check if the current element is already in the Map. If so, then just update the key (e.g. increment by 1). This way, you don't have to iterate the entire array (to avoid re-examining the same elements - duplicates). Finally, look for the largest value in the Map and grab the key. Time complexity is in the order of O(n) - much faster.

Map<Integer, Integer> mostFreqMap = new HashMap<>();

for(Integer key: a) {
{
Integer val = mostFreqMap.get(key)+1;
mostFreqMap.put(key, val);
}
else {
mostFreqMap.put(key, 1);
}
}

Integer largest = 0;
Integer freq = 0;
for(Integer key: mostFreqMap.keySet()) {
if(mostFreqMap.get(key) > largest) {
largest = key;
freq = mostFreqMap.get(key);
}
}

AG-bdwf
Автор

Thank you so much, comrade! I looked at a lot of videos to understand the most repetitive number in an array but I had never seen such kind of amazing video like yours.

jasur.ahmadoff
Автор

loved the line by line explanation! good job

TurtleSensor
Автор

damn dude. im glad i find your video. this is what i needed. thanks a million. im a beginner in coding and some of the other examples used crazy methods that i havent even heard of yet. You rock man!!

mattgarbeil
Автор

I love the explanation. I appreciate you and I just subscribed. I will def be coming back to your videos for the more advanced loops especially for arrays since that is where I am at in my course in college. Thank you!!!

brandonfigueroa
Автор

This tutorial saved me when trying to figure out some vaguely explained intro to java homework. Thank you!!

KoltonRoot
Автор

Great video, explained well, helped me with overall understanding of nested loops and their functionality. Thank you very much.

jackbower
Автор

Amazing video! As mentioned by someone else, how do we solve if we have two elements with the same frequency?

zacary
Автор

This video is a hidden gem. Thumbs up!

TomSeidel
Автор

I believe you can add extra array holding values that were already checked. Thus you will avoid counting frequency of element "2" 3 times in your case.
Brilliant explanation anyway.

bagzhansadvakassov
Автор

Thanks for the video!
It was good for me!
Greatly explained!
Greetings!

atanassirakov
Автор

Great video bro!! Very well explained.

How could u modify the program to say “array is multimodal” if there are two elements which both appear the most number of tjmes

datguy
Автор

well explained thank you
Francisco Iacobelli

inspired
Автор

If usage of HashMap is not restricted, why not using HashMap? You will not have to re-iterate the array again. This method would be slow for large arrays. Below method can find the most frequent number within single loop.


List array = new ArrayList();

Random random = new Random();

for(int i=0; i < 10000; i++){
int number = random.nextInt((10000 - 0) + 1) + 0;
array.add(number);
}

HashMap h = new HashMap();

int maxNumberCount = 1;

int maxNumber = 0;

for (int i =0; i < array.size(); i++){
int currentNumber = (int) array.get(i);

if (h.get(currentNumber) == null){
h.put(currentNumber, 1);
} else {
int numCount = (int)h.get(currentNumber) + 1;
h.put(currentNumber, numCount);
if (maxNumberCount < numCount){
maxNumberCount = numCount;
maxNumber = currentNumber;
}
}
}

System.out.println(maxNumber + " is repeated " + maxNumberCount + " times");
}

nebsar
Автор

Bro you are amazing. You saved our lives. But can you tell me what if i have two most frequent element (could be more) on my array and i want to print both of them.

rehabugrahanozel
Автор

Hi, Thank u very much for the video. I have an program here like if array a elements are a=[1, 2, 1, 5] and b array elements are b=[1, 3, 3, 4, 5] the output should be the repeating elements in both the arrays and there count. Output should be {1:3, 2:1, 5:2, 3:2, 4:1}

SaikiranPalugula
Автор

i think we could even not assign the element to temporary elements between the 2 loops and assign it directly
in final if statement because j is incremented automatically!!

hamzaelbensaidi
Автор

Hi can you help me that how to sort all the elements by using count values....
Need all the elements not only the maximum number hope you I’ll help me

praveenbinni
Автор

what if there are 2 integer elements with the same frequency? how do i state the condition so it would print "none" instead?

omorfia
Автор

can you explain if it has 2 most frequent element or more?

aaronfong