You can XOR all the elements to get the element which occurs odd no. Of times
@stewartzayat75267 жыл бұрын
Can you explain what you mean by that please?
@jayant36226 жыл бұрын
Xor of a number with itself is 0. For example consider the numbers are [2,2,8]. In this case xor(2,2) = 0 and then xor(0, 8) = 8 so the xor is the answer.
@stewartzayat75266 жыл бұрын
Ah, I see! I wouldn't have come up with that in thousand years
@souravmandal75276 жыл бұрын
you don't need to be concerned with the intermediate values it will be some complex bulshit but in the end all of them will cancel each other except for the number occuring odd number of times.
@sinharakshit6 жыл бұрын
@@Bdjdiwhbkdk If you XOR 2 with 1, you will get 3. Think about it this way - convert your decimal numbers 1 and 2 into binary. Now perform XOR on them. So XOR of 01 and 10 will give 11 in binary which is 3 in decimal. Now if you XOR 3 again with 1, you will get 2. Or if you XOR 3 with 2, you will get 1. Try it with the binary conversion and you'll see the proof yourself. That's the reason why we don't need to worry about the order in which the numbers are placed. If there are repeating numbers, they'll eventually "cancel" each other out in the end and you'll get the non-repeating number as the result.
@yasminadir6 жыл бұрын
First I want to say thank you very much for this video! I really appreciate it! I just wanted to say that in the second optimized solution you described, you said it will result in better than O(n) space complexity. I think that this is not entirely correct since you can have for example the array [1,2,3,4,5,6,7,8,7,6,5,4,3,2,1] - by the time you traverse the array and reach number eight you will have ~n/2 elements in the hash map thus still resulting in overall O(n) space complexity.
@dave1612566 жыл бұрын
Instead of using a hash table could you use a set. When you meet an element if it is not in the set, add it and if it is in the set remove it. At the end the set should contain one element which is the element that occurred an odd number of times
@ramrai26426 жыл бұрын
great! this would be clean data structure-wise
@wtfishappeninghere6 жыл бұрын
In some programming languages, a set is implemented as a hash table.
@wtfishappeninghere6 жыл бұрын
But I think using your method would save half the space in the worst case.
@treyquattro4 жыл бұрын
this would be the simpler solution but a set is likely to be implemented as a tree and a hash table as an array or bucket list, so you have a difference in O(logN) vs O(1) (big omega really) in terms of asymptotic analysis
@adityadubey52044 жыл бұрын
this is true when there is one element that occurs odd number of times and this is better
@IssaqAl-Ahmed7 жыл бұрын
That was an excellent video! I am sure many people will find value in this. Keep it up!
@congdoan20066 жыл бұрын
Thank you very much for your helpful video. In your code, the worse space complexity is O(# of distinct values in the input array). If values are integers then we can use XOR operator to have an elegant implementation, which is go through the array to perform XOR all the elements together. The end result is the odd recurring number. This is true because XORing any integer any even number of times yields zero whereas this will yield that integer itself for odd number of times.
@chaudharychaudhary64854 жыл бұрын
This method is quite good, If we trying to find even one. While for finding odd ones we can go for XOR.
@naktaal7 жыл бұрын
Thanks for taking the time to make this! The community could definitely use more material like this. I'm looking forward to seeing your future videos. The only recommendation I have is to get a microphone for the interviewer. She could barely be heard.
@IrfanBaqui7 жыл бұрын
Hi naktaal, I'm glad you liked it! Will make sure to get a mic for her too, thanks for the feedback!
@Code-fr8qx7 жыл бұрын
Excellent video. The only improvement would be a mic for the interviewer. It is also important for us to hear what he or she has to say, that way it simulates a real interview scenario. Regardless great video. Subbed.
@IrfanBaqui7 жыл бұрын
Thanks for the feedback! She'll be rocking a mic in the next one :)
@matto4836 жыл бұрын
not hearing half the content kind of kills the point, but good work.
@Patrickedoherty6 жыл бұрын
Brilliant videos and its excellent to see your raw thought processes. Thanks dude, I'm loving this channel.
@tannerbarcelos68804 жыл бұрын
The initial thought was use a hashmap, and return the key who’s value is odd. Which is fine. However that two separate loops. It all truncates to O(n), but it could be optimized further such that we only need one loop. And I think a good way of doing it would be to use a set that stores an element you have seen, and if you encounter it again in your traversal, remove it from the set and you would know that that’s been at least two times you’ve seen it and unless you see it again, that occurred evenly so we continue. At the end of the loop, you are guaranteed to have seen a value that never leaves the set, thus yielding your final answer which is just the first element in the set. You sacrifice some space, sure, but the time is better, and the space really wouldn’t be insanely hindered. Great optimization!
@samanrajaei81293 жыл бұрын
XOR would be the simplest as suggested by a few others. If I was doing your solution, I'd add the new 'unvisited' element to a stack, pop the element when I see it next and whichever elements stays in the stack at the end is your guy.
@jamezjaz3 жыл бұрын
Insightful! Thanks for sharing
@ThePolaris876 жыл бұрын
Excellent example! Will definitely subscribe to this series of videos.
@safiyapathan75107 жыл бұрын
Thanks for creating this video! It really gives me a good idea on how to structure a problem on the whiteboard. What do you think about covering other data structures and some advanced material?
@IrfanBaqui7 жыл бұрын
Glad you liked it! The first video is fairly easy because I want you to focus on the process rather than the problem itself. I'll cover moderate to hard problems in the next videos, so stay tuned!
@rrsqrd49327 жыл бұрын
Excellent video. I am preparing for this exact scenario. You explained your thoughts and solution to this problem very clearly. I definitely learned something new! thank you
@iftak016 ай бұрын
Thanks 🙏 😊
@debdutsaha43164 жыл бұрын
at first we can sort the array in O(logn) and then we can iterate through the sorted array and count the occurence of the numbers and then whenever we find number of occurence is odd then we can break the loop in case of hashmap it is taking space complexity as well. So overall in worst case senario it is O(n) not as mensioned 2O(n).
@thatbeastabdul7 жыл бұрын
Love the video, man! One suggestion I have is to just use a HashSet instead of a HashMap since a key only exists in the map with an associated value of 'true', so the mapping becomes redundant. Other than that, great video and I look forward to the rest of the videos in this series.
@IrfanBaqui7 жыл бұрын
Thanks for the feedback, Abdul! That's definitely another way to do it.
@KevinFash7 жыл бұрын
Very Helpful. Hope you continue making these.
@IrfanBaqui7 жыл бұрын
I'm glad you found it helpful, Kevin. There's more to come, so make sure you subscribe :)
@affanshaikh53535 жыл бұрын
O(n) time complexity and O(1) space complexity Just xor all the elements remaining one will be the answer!
@vsigal4 жыл бұрын
Odd-way number is unique. Do not need to iterate huge array - break the loop. In Python3: for x in set(numList): if numList.count(x)%2 : print(x) break
@smontiu6 жыл бұрын
It´s not neened to loop the hashtable, instead use a sum, in case of repeated number sum it´s negative, at last you will have the return value
@mereum96695 жыл бұрын
Why do we need to decrease the number of keys in hashmap? Space complexity is defined by the largest amount of memory that is used at certain time during the program, so the last modification to shorten a hashmap is not doing anything in this case, is it?
@crothert6 жыл бұрын
thank you so much for this free content you are king xD
@philbowden5 жыл бұрын
That was great thank you! Wish I could have heard the interviewer...but oh well:-)
@HarshilLodhi7 жыл бұрын
Great explanation. Is the expectation of a O(1) space complexity valid for this question. I am thinking of a solution using XOR
@IrfanBaqui7 жыл бұрын
Thanks! And yes, that's valid.
@youcan49136 жыл бұрын
Write a code to.find a balance point in any array with time complexity of O(n) and a constant space complexity.
@adrianharo65865 жыл бұрын
Balance point?
@TeeheeTummytums5037 жыл бұрын
Thanks for the video, friend.
@alexsky887496 жыл бұрын
Excellent interview training video! I learned a lot on how to behave and explain.The logic is fluid and good too.
@farhad52875 жыл бұрын
the space complexity of your solution is not O(1) if the question asks so. Example: [1,2,3,2,3]
@neerajkungwani62956 жыл бұрын
Awesome !! Keep doing the good work
@francisaiello61975 жыл бұрын
Great idea Irfan - my only comment would be that in the future - I would repeat the question (difficult to hear interviewer)
@rolandgavrilescu30996 жыл бұрын
Very helpful, thanks!
@fmango5 жыл бұрын
hmmm how would you code this in java tho?
@bensmith92536 жыл бұрын
Great video
@georgepetrosyan45894 жыл бұрын
I think use some additional functions like python funcs to check number is not correct. My first solution was use hashmap int int, Second same as first but hashmap int bool, Third use unordered set to add/remove number(solution 2 of this boy) One more. May be use first of all sort function, an then go throw the array ,get first number,set flag to true then go until next value will not be equal on each step change flag true to false and false to true. If in some step i, arr(i)!=arr(i+1) check flag if it true then value is odd and return, if it even then continue.
@Jubanjan6 жыл бұрын
with python dictionary this becomes dead easy! Is that allowed in interview?
@parvezrafisparta6 жыл бұрын
Great irfan!!!
@PraneethPeiris5 жыл бұрын
Let me explain my answer using XOR. If we consider any two integers in binary form, XOR operation on itself is 0. i.e. 6 = 0110 0110 0110 ^ --------- 0000 ===== So, if there are even number of occurrences of a given integer, it will produce 0 when it's XORed. When you XOR an integer with 0 (resulted from the even number of integers), you will still get the same number. i.e. 5 ^ 0 0101 0000 ^ -------- 0101 ==== Which means when you keep on applying XOR on all the integers, the integer with an odd number of occurrences will remain, irrespective of the order of the integers. Example: 3, 1, 3, 2, 3, 1, 2 (XOR is done from left to right) 11 ^ 01 ^ 11 ^ 10 ^ 11 ^ 01 ^ 10 10 ^ 11 ^ 10 ^ 11 ^ 01 ^ 10 01 ^ 10 ^ 11 ^ 01 ^ 10 11 ^ 11 ^ 01 ^ 10 00 ^ 01 ^ 10 01 ^ 10 11 The answer is 11 in binary = 3 in decimal.
@jamesmay19004 жыл бұрын
SELECT a.val AS [oddly repeating value] , SUM(COUNT(a.val)) AS [count of repetition] FROM array as a GROUP BY a.val HAVING MOD(SUM(COUNT(a.val)),2) > 1
@TeCHnoMonkeys7 жыл бұрын
Great vid
@ErikaJadeLives5 жыл бұрын
So if all numbers in the input array are repeating and one of the numbers repeats an odd number of times.... then the number we are looking for has to have occurred at least 3 times.... In other words, there isn't a number that only occurs once. In his example for input... the number that occurs an odd number of times only occurs once, so this would be an invalid example. Then in the code you would need to account for this. The number of occurrences must be >= 3 for the number that occurs an odd amount of times. Did anyone else catch this? What do I know though.. Just a new developer still trying to get a job. LOL!
@rizzwanofficial63736 жыл бұрын
if no of any of digits would be 3 like [ 1, 4, 1, 4, 1, 6, ] then what will be the result
@charulsaxena42425 жыл бұрын
I would also be interested in the reply. There can be a scenario where even 2 of the numbers in the array are occurring odd time. What is the result then. Where are we checking that.
@guitarockdude6 жыл бұрын
Space Complexity: O(n) Time Complexity: O(n)
@devyashsanghai5856 жыл бұрын
Another solution, If I am not wrong would be to take and xor sum of all the number in the array. It will return the number that is repeated odd number of times.
@ramrai26426 жыл бұрын
Sounds like this is most optimal so far: i.e XOR of all elements together is the answer
@rafaelcupello25497 жыл бұрын
Nice!!!!!!!!!!!!!!!!!!!
@jonjrodriguez7 жыл бұрын
Thanks for the video. I subscribed but do have some feedback. 1. The code you wrote isn't valid javascript. I know you said it was pseudocode at the end, but from my experience the code should be valid (or at least close). 2. Final solution wasn't the optimal solution (can be done in O(n) time and O(1) space). I know it's the first time you're seeing the question and we don't all get the optimal solution, but it would be good to quickly talk about the optimal solution in the end review. Thanks again and looking forward to more.
@IrfanBaqui7 жыл бұрын
Jonathan Rodriguez Thanks for the feedback!
@shafidayatar42496 жыл бұрын
Why not use HashSet instead. Logic is very good!
@arjuns.37526 жыл бұрын
Sort the array using inbuilt function and count frequency of each number in 1 for loop.if ..... Count%2!=0 Print number and then break loop