Please help me with following code I am not able to understand why I am getting wrong output

@harshulgarg21 your code is not considering the frequency of the elements which is a very important aspect in this question. For eg if there are 2 2 in arr1 and 3 2 in arr2, your code will print ALL the 2’s present. But you only have to print the “common” ones, which means print it the max number of times it occurs in BOTH the elements. In this case the max it occurs in both the elements is 2 times.
What you can do is, you make a frequency array for both the arrays freq1 and freq2 and print a particular element min(freq1[i], freq2[i]) times

can you please make the necessary changes in my code

@harshulgarg21 the approach is quite different than what your code is currently using, so I suggest writing it from scratch. I have explained the use of freq array above, if you are still confused please let me know.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.