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 8708441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:08:18+00:00 2026-06-13T04:08:18+00:00

I want to find similar objects in a collection depending on a Method I

  • 0

I want to find similar objects in a collection depending on a Method I implement

for example this sample class:

class myObj
{
    public int Data1 { get; set; }

    public int Data2 { get; set; }

    public int Data3 { get; set; }        
}

then implement Similar method in the class:

public bool Similar(myObj obj)
{
    if (obj.Data1 == this.Data1 && obj.Data2 == this.Data2)
        return true;
    return false;
}

now I have this collection:

List<myObj> items = new List<myObj>();
// none similar
items.Add(new myObj() { Data1 = 1, Data2 = 2, Data3 = 4 });
items.Add(new myObj() { Data1 = 2, Data2 = 3, Data3 = 18 });
items.Add(new myObj() { Data1 = 3, Data2 = 4, Data3 = 75 });
items.Add(new myObj() { Data1 = 4, Data2 = 2, Data3 = 3 });
//similar
items.Add(new myObj() { Data1 = 5, Data2 = 26, Data3 = 97 });
items.Add(new myObj() { Data1 = 5, Data2 = 26, Data3 = 37 });
items.Add(new myObj() { Data1 = 10, Data2 = 45, Data3 = 47 });
items.Add(new myObj() { Data1 = 10, Data2 = 45, Data3 = 19 });

to get the similar objects I did this:

private static List<myObj> GetSimilars(List<myObj> items)
{
    List<myObj> similars = new List<myObj>();
    while (items.Count > 0)
    {
        var q = (from c in items
                 where c.Similar(items[0])
                 select c).ToList();

        if (q.Count > 1)
        {
            similars.AddRange(q);
            foreach (var obj in q)
                items.Remove(obj);
        }
        else
            items.Remove(items[0]);
    }    
    return similars;
}

is there a better way to do that?

  • 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-06-13T04:08:19+00:00Added an answer on June 13, 2026 at 4:08 am

    How about making this class, which is reusable.

    public class MyObjSimilarity : EqualityComparer<myObj>
    {
        public override bool Equals(myObj a, myObj b)
        {
            if (obj.Data1 == this.Data1 && obj.Data2 == this.Data2)
            {
                return true;
            }
    
            return false;
        }
    
        public override int GetHashCode(myObj o)
        {
            int hash = 17;
            hash = hash * 23 + o.Data1.GetHashCode();
            hash = hash * 23 + o.Data2.GetHashCode();
            return hash;
        }
    }
    

    You could use like this,

    var similarity = new MyObjSimilarity();
    items.Where(o => similarity.Equals(o, w));
    

    or pass into the constructor of a dictionary,

    var similarity = new MyObjSimilarity();
    var lookup = new Dictionary<myObj, string>(similarity);
    

    or in a GroupBy

    var similarity = new MyObjSimilarity();
    items.GroupBy(o => o, o => o, similarity);
    

    or like the other answer

    var similarity = new MyObjSimilarity();
    items.GroupBy(
        o => o,
        o => new { Instance = o, Count = Count(o) },
        similarity);
    

    or in other framework friendly places.

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

Sidebar

Related Questions

Java has a very simple way to serialize objects. couldn't find something similar in
I have a point class like: class Point { public: int x, y; Point(int
I have a collection (list) of objects. I want to be able to sort
One example of the general case: public class Validator { // ... public ReadOnlyCollection<InvalidContainer>
I want find all Saturdays and Sundays in A given month. How can I
I want to find a struct in a vector, but I'm having some troubles.
I want to find the sum and count of specific values in my table.
I want to find out the position of an element which is in iframe.
I want to find the Xelement attribute.value which children have a concrete attribute.value. string
I want to find files containing the word navbar anywhere in files. I can

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.