tags:

views:

25

answers:

2

I have a class having 2 properties (both integers) and storing class into List collection.

Public Class GroupSelect
Public Property RowNo() As Integer
    Get
        Return m_RowNo
    End Get
    Set(ByVal value As Integer)
        m_RowNo = value
    End Set
End Property
Private m_RowNo As Integer
Public Property GroupNo() As Integer
    Get
        Return m_GroupNo
    End Get
    Set(ByVal value As Integer)
        m_GroupNo = value
    End Set
End Property
Private m_GroupNo As Integer
End Class

Ex:

RowNo     GroupNo
1          1
2          1
4          2

How to find each item in "GroupNo" has more than ONE TIME. In above example 2 is only one time, So return 'FALSE"

RowNo     GroupNo
1          1
2          1
4          2
5          2

Here I need return "TRUE". (values in "GroupNo" is not static and not in sort).

How can we do that in vb.net?

A: 

I might be misunderstanding, but does RowNo and GroupNo have a 1 to 1 relationship? Will index i of RowNo always be compared to index i of GroupNo? Do we even care about RowNo?

It sounds like the question you're asking is how to determine if an item in GroupNo is never repeated. Your first example shows 2 only once (which you want to return FALSE) and in the second it's shown twice (which you want to return TRUE).

If we can disregard the RowNo, a very simple way to determine if something is only listed once would be to sort GroupNo and then perform a loop and check index i with index i+1. You can keep a counter within the loop that counts the number of times that number is repeated. If index i is not equal to i+1 and the counter is at 1, you know that the number is never repeated.

Did I misinterpret the question?

Eclyps19
I am using RowNo for remove item from arrayList. It always unique.
James123
If RowNo is acting as an incremental primary key and GroupNo is the data, then I would put them into a multidimensional array. That way you can delete a single ordered pair (x,y) rather than removing one from each array. There's a lot of room for error if you keep them separate.This would make my solution a bit harder, since the sorting isn't as simple on multidimensional arrays, but once the concept is still the same. Once your loop finds an item that isn't repeated, just return false. When are you removing values? Is this done based on the boolean condition?
Eclyps19
A: 

So you want to the operation to return true if there are 2 or more RowNo values for every GroupNo otherwise return false? If that is correct then the following function will do that.

Note: Since you used the C# tag I feel like I have the green light to post code in C#.

public static bool HasTwoOrMoreInEachGroup(IEnumerable<GroupSelect> enumerable)
{
    var groups = new Dictionary<int, int>();
    foreach (GroupSelect item in enumerable)
    {
        int count = 0;
        if (groups.TryGetValue(item.GroupNo, out count))
        {
            groups[item.GroupNo] = count + 1;
        }
        else
        {
            groups.Add(item.GroupNo, 1);
        }
    }
    foreach (int count in groups.Values)
    {
        if (count <= 1)
        {
            return false;
        }
    }
    return true;
}

}

Or the Linq solution...

bool result = list.GroupBy(x => x.GroupNo).Where(x => x.Count() == 1).Count() >= 2;
Brian Gideon