Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3359696
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:54:13+00:00 2026-05-18T02:54:13+00:00

I am using a HashSet. I want to ensure that I don’t have duplicate

  • 0

I am using a HashSet. I want to ensure that I don’t have duplicate values. However, I am able to do so at the present moment.

[Serializable()]
public class SafetyObsDS
{
    public int Count
    {
        get { return set.Count; }
    }

    HashSet<CompResults> set;

    public SafetyObsDS()
    {
        set = new HashSet<CompResults>(new CompareToComparer());
    }

    public bool Add(CompResults cr)
    {
        set.Remove(cr);
        return set.Add(cr);
    }

    public bool Remove(CompResults cr)
    {
        return set.Remove(cr);
    }

    [Serializable()]
    private class CompareToComparer : IEqualityComparer<CompResults> 
    {
        public CompareToComparer() {}

        public bool Equals(CompResults cr1, CompResults cr2) 
        {
            return (cr1.CompareTo(cr2) == 0);
        }

        public int GetHashCode(CompResults cr)
        {
            return (cr.SectionID);
        }
    }
}

[Serializable()]
public class CompResults : IComparable
{
    public int SectionID { get; set; }

    public ComResults(int sectID)
    {
        SectionID = sectID;
    }

    public int CompareTo(object obj)
    {
        return SectionID - (obj as CompResults).SectionID;
    }

}

I left the constructors and additional unnecessary code such as other fields out
When I perform the following operations, I receive undesired results:

Add(new CompResults(1));     Result: true
Add(new CompResults(1));     Result: true  <--- this is a dup and I don't want it to add
Remove(new CompResults(1));  Result: true

thanks for your help in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:54:14+00:00Added an answer on May 18, 2026 at 2:54 am

    I believe the problem is that in your Add() code, you first remove the existing item, then add the new (duplicated) one. This will always be successful since there aren’t any duplicates which is why it returns true. Remove the Remove() call and it will fix itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At the moment I am using a custom class derived from HashSet . There's
I have a test where I want to ensure separate operations within one page
I have this class: using System; using System.Collections.Generic; using System.Runtime.Serialization; namespace Grouping { [Serializable]
I'm getting back into using MonoTouch, and I tried using HashSet<int, List<MyClass>> I have
if I'm using the Scala Multimap, and I want to get the values associated
Quick background I have a concurrent map I used to cache some values that
I have developed one application which can using hashset concept. I have to override
Can anyone tell me how is ObjectIDGenerator better (worse?) then using HashSet when traversing
when scanning a file for words and using the built-in hashset class from the
using chrome 15 on osx 10.6.8 When adding event listeners, I've found that putting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.