Hi All,

I have written a program which contains a class 'Numbers' which can hold a set five numbers. I then create 2 List<Numbers> instances which generate a random numbers. I then loop through one list and try to use linq to find if the list has an identical set of numbers.

var foundLine = LineList.Find(a => (a.Ball1 == line.NumberOne || a.Ball1 == line.NumberTwo || a.Ball1 == line.NumberThree || a.Ball1 == line.NumberFour || a.Ball1 == line.NumberFive) && (a.Ball2 == line.NumberOne || a.Ball2 == line.NumberTwo || a.Ball2 == line.NumberThree || a.Ball2 == line.NumberFour || a.Ball2 == line.NumberFive) && (a.Ball3 == line.NumberOne || a.Ball3 == line.NumberTwo || a.Ball3 == line.NumberThree || a.Ball3 == line.NumberFour || a.Ball3 == line.NumberFive) && (a.Ball4 == line.NumberOne || a.Ball4 == line.NumberTwo || a.Ball4 == line.NumberThree || a.Ball4 == line.NumberFour || a.Ball4 == line.NumberFive) && (a.Ball5 == line.NumberOne || a.Ball5 == line.NumberTwo || a.Ball5 == line.NumberThree || a.Ball5 == line.NumberFour || a.Ball5 == line.NumberFive));

For testing I hard coded the two list so:
LineList would contain (1,2,3,4,5), (4,3,2,7,1),(222,122,1,33,44).
line would contain (1,2,3,4,66),(22,22,33,11,346),(23,33,66,88,44)

So from looping though line. I should expect to see no match with the piece of code above, however im getting the nearest thing, in this case (1,2,3,4,66) corresponding to (1,2,3,4,5).

Does anybody know what im doing wrong, or even a better way of comparing list as im comparing against as many as 40k of elements.

Thanks

Recommended Answers

All 4 Replies

If I understand you well, I think you can do something with the intersect method. If not, somewhat more explanation would be welcome.

All im really after is a boolean to comeback true if a line of numbers in one list, belongs in another despite the order of numbering.

So using intersect, I dont think this works for what im acheiving.

This is a sample of something ive done using intersect:

` static void Main(string[] args)
{

        var lineOneList = new List<line>();

        var line1 = new line();
        line1.One = 1;
        line1.Two = 2;
        line1.Three = 3;
        lineOneList.Add(line1);

        var line2 = new line();
        line2.One = 11;
        line2.Two = 22;
        line2.Three = 33;
        lineOneList.Add(line2);

        var lineTwoList = new List<line>();

        var line3 = new line();
        line3.One = 133;
        line3.Two = 244;
        line3.Three = 3111;
        lineTwoList.Add(line3);

        var line4 = new line();
        line4.One = 2;
        line4.Two = 1;
        line4.Three = 3;
        lineTwoList.Add(line4);

        IEnumerable<line> both = lineOneList.Intersect(lineTwoList);

        foreach (line id in both)
            Console.WriteLine(id);

    }
}

public class line
{
    public int One;

    public int Two;

    public int Three;
}`

This code doesnt write anything to the console. For the sake of my program, line 1 and 4 are a match. So with my code from the previous statment, I would expect a line to comeback. A true/false would be just a good.

Took me a while, but here is my try with HashSet:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleBallNumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            List<HashSet<int>> LineListOne = new List<HashSet<int>>();
            LineListOne.Add(new HashSet<int> { 1, 2, 3, 4, 5 });
            LineListOne.Add(new HashSet<int> { 4, 3, 2, 7, 1 });

            List<HashSet<int>> LineListTwo = new List<HashSet<int>>();
            LineListTwo.Add(new HashSet<int> { 1, 5, 4, 3, 2 });
            LineListTwo.Add(new HashSet<int> { 22, 22, 33, 11, 346 });

            foreach (HashSet<int> Set1 in LineListOne)
            {
                foreach (HashSet<int> Set2 in LineListTwo)
                {
                    if (Set1.SetEquals(Set2))
                    {
                        Console.WriteLine("Equal sets found.");
                        Print(Set1);
                        Print(Set2);
                    }
                }
                Console.ReadKey();
            }
        }

        static void Print(HashSet<int> H)
        {
            foreach (int i in H)
            {
                Console.Write(i);
                Console.Write(' ');               
            }
            Console.WriteLine();
        }
    }
}

May the Force and HashSet be with you!

Thanks i used HashSet to get what I wanted. Seems to do the trick and I cant see much impact on performance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.