- 2 days ago
Transcript
00:00Next time solve this question is which name is Majority element. Majority means Maximum or which is the quantity or
00:07frequency. Now we have an integer array given which is size n and which we will get many elements in
00:14which we will get in many elements. I will take out those elements whose frequency
00:18or which appear in my array more than n by 3 times. For example, I take this nums array. I
00:25have given numbers 1,3,2,5,1,3,1,5,1. Now I can see how many times I
00:31have 1,2,3,4 times. How many times I have 2 times? 1 times. How many times I have
00:403 times? 3 times. In my array 2 times. If I talk about 5, 5 also appears.
00:47two times. Now for this nums array, what will my n value be? Total number of elements? 3, 4, 5,
00:546, 7, 8, 9. So n by 3 value will be 3. So I have to take those elements that
01:01n by 3 will be more times. I mean, you will come more than 3 times in my array. And
01:05this is a element that comes 4 times. So my answer will be 1.
01:11If I have a second example in which my nums array is 1 and 2. Now, what frequency will be
01:161? What frequency will be 1? How many times it will appear in my array? If I talk about frequency,
01:22that will also be 1. Now, if I remove N for my array, that will be 2.
01:27n divided by 3 will be 2 divided by 3 which is equal to 0 in integer terms. So I
01:33will print in my array basically those elements which will be more than 0. Now 1 will be more than
01:390 and 2 will be more than 0. That's why I will answer 1 and 2.
01:43So basically in this question, the work is that in which way I can track the frequency with my numbers.
01:52For this work, we will use hash maps. And hash maps can be used very much in frequency questions. So
01:59many of you will get frequency related, count related questions. In fact, we also get many questions from sub-array
02:04related to sub-array, which we track the frequency.
02:16and frequency pair store. We use the map. In this question, I am going to go to the index and
02:24for each number, I will store the frequency in the map. In the beginning, we will take a blank map.
02:31When I click 1, then what will store my map? Number 1 and frequency 1. After that, I am coming
02:37to my index.
02:393. When the index 3 came, I will see what 3 maps already exist. If I don't do it, I
02:44will store it. I have stored 3 with a frequency of 1. The key is my number which will store
02:51once a time. If I have 3, it will store once a time. What can I do? I can increase
02:55its frequency if it will come again. Or I can increase its count. This will be my frequency or I
03:02can call it my count.
03:04After that, I am coming to 2. I will check if 2 already exists in the map. If 2 exists
03:10in the map, I will store 2 with a frequency of 1. After that, I am coming to 5. I
03:16will check if 5 already exists or not. I will store it with a frequency of 1.
03:21After that, I will come to 1. I will check if 1 already exists. In this case, 1 already exists.
03:28What is the frequency of 1? 1 is 1. My 1 is 1 more time. Now, I have to increase
03:33this frequency and I have to do 2. That means, my 1 has been 2 times now. What am I
03:39going to do? I will remove this 1 from here. And I will store this 2. How can the pseudo
03:45code be done? I will run a loop. For i is equal to 0.
03:51to my array.length or nums.length and check every time for your number. My number number is equal to
04:00array of i. So in that case, I will check that if map.contains number, I will pass the number
04:10in the map. If already number exists in that case, what is it? In that case, I will update the
04:17value in the map. What value should I update? I have to put for number.
04:22map.get number plus 1. What does this line mean? If my key already exists, like here 1 exists, then
04:32what do I do? I will put that number again, that means 1 with a frequency of the first frequency
04:38of map.get number and I will add 1 to that. So the first frequency of 1 will come and
04:43add 1 to that.
04:46And in the else case, if that number doesn't exist, then I will put my number in the map with
04:53a frequency of 1. For the first time, if the number is coming, then we will add 1 to 1.
05:00So we will write code in this way. If I go to index 3, we will check that already exists.
05:09So what will I do? I will add 1 to 1 in the previous frequency.
05:15which was 1 and here will come 2 as my new frequency. After that, I will come to the next
05:21index. What will happen for 1? We will check again if we exist. 1 is my key exists. I will
05:27add 1 to the previous frequency. If I add 1, this will come to the previous frequency and here will
05:34come to the next index. 3 as my new frequency. After that, I will come to the next index. We
05:38will check for 5. 5 already exists. So we will add 1 to the previous frequency.
05:43I am going to remove this and it will come here, 2. After that, I will come to the next
05:47value, 1. 1 already exists in my map. What is 1? 1 key is 3. Now it will increase its
05:54frequency from 1 to 1. That means I will come to 4 to equal. The whole array I have traversed.
06:01Now I have to iterate on which I have to iterate on. I have to iterate on which I have
06:05to iterate on the map. I have to iterate on the map. Every time I will check on the map.
06:13Map.GetAllMyKeys. I can get a key from the key set. We have already learned how to iterate on the
06:18map. So I will get a key set first and I will get all the keys from it. Which is
06:22the key frequency, meaning Map.GetKey, if greater than n by 3, I will print my key set.
06:30key. So in some way my pseudo code will be done in which the first loop will work in which
06:36the second loop will be done on the key set in which I will do iteration. Now I will see
06:39which key is in the map which is greater than n by 3. Obviously only one key means this is
06:45which frequency is 4 which is greater than my n by 3. So I will print my one here.
06:52So that's why we write code. And this code will run in big of n. That means I have constant
06:57time. Because in terms of time complexity, when we talk about hash map operations, we consider them constant. So this
07:05which I am checking the contains, this which I am checking the put operation, these are constant operations. In fact,
07:11this is going to print key or get key, these are also constant operations. Where did I find the operation
07:15major time? In this loop. Or in this keyset loop.
07:20That's why the time complexity will be linear which is big of n. If I don't do the question of
07:24majority element in the hash map then what is the brute force approach? The brute force approach says that every
07:30time
07:31I will pick up some element and then I will run a nested loop in which I can see that
07:35this element is in the other way, where and where and where and where. In this case, I feel n2
07:40time complexity. But because I have stored my frequencies in hash map, my n2 brute force time complexity will convert
07:49into big of n. So in this way, in any code, we have to do optimizations in terms of time.
07:55So we help us with those optimizations, hash maps and hash sets.
07:59I have my classroom class in which I have imported my Java package. In the main function, I have my
08:05array given. First of all, what will happen? First of all, we have to initialize a hash map. So, hash
08:11map type integer and integer. Because my key also has an integer type and my value also has an integer
08:18type. I will name it map is equal to new hash map. I will write a loop for int i
08:26equal 0, i less than array dot length i plus plus.
08:30What will I do every time? I will check every time what the map already exists. If my ARR already
08:38exists in the map, I will put it in the map.put my ARR and map.get my number is
08:49plus 1. What happened here? I have added the same key in the map.
08:54But with an increased frequency. How much did it increase? It increased the entire frequency from 1. And if it
09:01doesn't happen, in that case, I will put my number with a frequency of 1. What do I have to
09:07do now? In last, I have to run a loop again. To run a loop, I can write a statement
09:16here that set of my integer keyset is equal to map.keyset. And after that, I will run a loop.
09:27this is my advanced for loop, for loop ke an dar kya hooga,her baar mein ek integer type ki
09:33key nikaal loongi from my keyset.
09:36We know that this for loop or for each loop how to write this syntax we have already seen a
09:41lot of times when we have to traverse on a collection of objects. So I can write this code or
09:47I will show you some shortcuts too. So what do I do? As I have a key in my key
09:53set, now I have to print that key which is greater than n by 3.
10:05array.length by 3. So, in this case, I will print my key. Let's do this one, save and run.
10:18I have a single key.
10:21which is one, because one is just like which frequency is 4 and the rest of the frequency is less.
10:26Now, to optimize the code here, just to look at it, I'm going to teach you some things, which we
10:34can use when we practically coding maps. This is the whole process of 4 lines, we can convert it in
10:412 lines. This is the process, I can convert it in some way, because I can write map.put
10:49Because what do I want to do? If the condition is true, then I want to put something in the
10:53map. And if the condition is false, then I want to put something in the map. So we can create
10:59a short hand, which we can directly put in the map. Just let me see that this key is the
11:04same as the value. So first of all, I will add map.put my key. That's enough.
11:12Code is the same. This part already exists here and here exists. Now I have to see the value of
11:18the part. So in maps exists a function in maps which is get or default value. I will write map
11:24.get or default
11:28What does Map.Get or Default mean? Map.Get is the function of this function. We have to get this
11:33value. We have to get a key and I will get my value. But if I don't exist in the
11:39value map, what does the normal get function mean? I will return null. But what does this function mean? I
11:45will return a default value. And I can also pass that default value as a parameter. What will my key
11:52happen? Which key I want to get? I want to get it for array of i.
11:57but if you don't exist area of i, then what do i need? i need 0, so if the number
12:03doesn't exist, then how much is the frequency? it's 0, its count is 0, because it doesn't exist, so if
12:12it's valid frequency, then i get 0, then i get 0.
12:17This is the get or default function. If I have a key exists, I will get the value of it.
12:22If not, I will get the default value of it. This is zero, which I can pass from this function.
12:28Now, any frequency will return. Or 5 frequency will return, 3 will return, 2 will return, and if it doesn't
12:34exist, then 0 will return. Whatever I have done, I will add plus 1.
12:39If it has returned 2, it will add 1 and the frequency will add 3, which is the right case.
12:44If it doesn't exist and it has returned 0, then I will add the frequency of 1, which I was
12:49doing directly here. So what happened? I removed these 4 lines and I simply removed my code in a single
12:57line. If I save and run it, I will have the same output. Which is equal to
13:03Now I will show you a short cut here. I have stored my keyset separately. What did I do? I
13:10called the map.keyset function which has given me a set name, a data structure, which is stored in my
13:16keys. And then I have stored a loop on it. So why do I store it separately? I can write
13:22it directly in the loop. So this is the map.keyset line. I will cut it from here and copy
13:28it.
13:29and I will completely remove this line. So I need to store this data structure separately. So I can save
13:35it directly. And I can do the same thing because I have an answer to the same. So I have
13:42learned two shortcuts in maps. The first shortcut is that the get function has a get or default function. If
13:48it exists, it gets a normal value. And if it doesn't exist, it gets a default value. So our short
13:54code can become small.
13:56In the second thing, we learned that I don't need to store a separate key set every time. If I
14:01just want to run a loop, then I can simply use the map.keyset. My big code is now small
14:07and the whole time complexity of this code is big of n.