Why is my HashSet in c# allowing duplicates, please help.
Anonymous User
1012

public IList<IList> ThreeSum(int[] nums) {
HashSet<IList> hset = new HashSet<IList>(); // Created a HasSet
Array.Sort(nums); // Sorting the numbers
int n = nums.Length;
if(nums[0] >= 0 | n < 3) // Checking if the array length is less than 3 or first element is a positive number.
{
return hset.ToList();
}

    for(int i=0;i<n-2;i++)
    {
        int j = i+1;
        int k = n-1;
        while(j < k)
        {
            int sum = nums[i]+nums[j]+nums[k];
            if(sum == 0)
            {
                List<int> list = new List<int>{nums[i],nums[j++],nums[k--]}; // Adding values to a integer list first.
                hset.Add(list); // Adding to a hashset to remove duplicates.
            }
            else if(sum > 0)
                k--;
            else if(sum < 0)
                j++;
        }        
    }
    return hset.ToList();        // Converting back to an IList from HashSet     
}

// This program is running fine but not removing the duplicate values from the list even though I am using a HashSet. Appreciate your help.

Comments (1)