Why is it not working if i use hashset in java


public class Solution {
public int removeDuplicates(ArrayList a) {
HashSet set = new HashSet();
for(int num : a){
if(set.contains(num)) continue;
return set.size();


You have to change the input array also. But in your code you are just returning the number of distinct elements
For eg [1,1,2] will return 2 ( as per your code) which is the number of distinct elements but when you print the first two numbers from the array you will get [1,1], which is wrong.