How to find duplicate elements in array in effective way? I mean to say with very less iterations

You can use a HashSet because Sets don’t allow duplicates, just loop over array of elements and insert them into a HashSet using the add() method. If the add() method returns back false then that element already exists in the set and it is there for your duplicate. This way you only loop over the array once which results in a time and space complexity of O(n).

Leave a Comment