Doubt in Arrays repeating elements

https://ide.codingblocks.com/s/424615
what is wrong n my approach

Hello @Abhay-Chauhan-1546158815521966 what is the exact question you have to remove the duplicate elements or to print those which have duplicacy?

print duplicate elements

@Abhay-Chauhan-1546158815521966 check this i have corrected and commented the mistake:


Happy Learning!!

https://practice.geeksforgeeks.org/problems/find-duplicates-in-an-array/1/?category[]=Arrays&category[]=Arrays&company[]=Amazon&company[]=Amazon&problemStatus=unsolved&problemType=functional&page=1&sortBy=submissions&query=category[]Arrayscompany[]AmazonproblemStatusunsolvedproblemTypefunctionalpage1sortBysubmissionscompany[]Amazoncategory[]Arrays

this is the problem solution you sent is still not working

@Abhay-Chauhan-1546158815521966i have checked the question and constraints now. acc. to gfg constraints your approach is not optimised.
check this approach:

  • Approach: The basic idea is to use a HashMap to solve the problem. But there is a catch, the numbers in the array are from 0 to n-1, and the input array has length n. So, the input array can be used as a HashMap. While Traversing the array, if an element ‘a’ is encountered then increase the value of a%n‘th element by n. The frequency can be retrieved by dividing the a % n’th element by n.
  • Algorithm:
    1. Traverse the given array from start to end.
    2. For every element in the array increment the arr[i]%n‘th element by n.
    3. Now traverse the array again and print all those indexes i for which arr[i]/n is greater than 1. Which guarantees that the number n has been added to that index
    4. This approach works because all elements are in the range from 0 to n-1 and arr[i] would be greater than n only if a value “i” has appeared more than once.

@Abhay-Chauhan-1546158815521966 if you have any doubt you can ask here:

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.