tags:

views:

135

answers:

3

I have to check duplicate records in ArrayList. In ArrayList each item should have atleast 2 times before insert new Item. Per example

Example 1:

AL(0) = '1'
AL(1) = '1'
AL(2) = '2'
AL(3) = '2'
AL(4) = '2'
Method has to return = True, because each value has atleast 2 times in the list.

Example 2:

AL(0) = '1'
AL(1) = '1'
AL(2) = '2'
AL(3) = '3' //Trying to insert new Item, But It should not

Method has to return = 'false', because '2' has 1 time in the list. So I dont 
want insert '3' in ArrayList and return false. 
+2  A: 

I do not know much VB.Net, but maybe the following C# code might help (using LINQ).

array.Distinct().All(item => array.Count(other => other == item) > 1)

I'm guessing the VB syntax (might be wrong)

Array.Distinct().All(Function(item) Array.Count(Function(other) other = item) > 1)

Here array holds the list item items you're interested about

sukru
what is array.Unique() ???are you trying to refer array.Distinct() instead?
Anindya Chatterjee
yes that's correct -- fixed
sukru
Still wrong though, ArrayList isn't generic, and doesn't have most extension methods. You need `.Cast<char>` or `.OfType<char>`, or the native `ArrayList.ToArray()`.
Kobi
@Kobi - Man I hate `ArrayList`. They need a loud alert that says use a generic list.
ChaosPandion
@ChaosPandion - well, in their defense, you don't have `using System.Collections` by default anymore, but it is surprising to see so many of these questions.
Kobi
@Kobi - Let me just add an addendum to my previous comment. I am not really that religious about `ArrayList`. It is just an annoyance for me.
ChaosPandion
@ChaosPandion - good to know. You sounded quite angry `:)`
Kobi
I had not used AL for such a long time, I had forgotten it's not generic. :( Another (probably better) choice would be using List<T> instead of ArrayList in the first place. No casts, no worries :)
sukru
A: 

Use the following check on arraylist to check if there are duplicate data or not in the arraylist

   return arrList.ToArray().Distinct().Count() == arrList.Count;

if you want to check before inserting that the data is already present in the arraylist or not use the following check

if (!arrList.Contains(data)) {
    arrList.Add(data);                
}
Anindya Chatterjee
Those are both good options, but unfortunately the OP wants something else; only insert new items if the prev item exists more than once.
Kobi
+1  A: 

If your ArrayList is ordered, and contains strings of numbers (as illustrated in your post,) then the below function should work:

Private Function OKToInsertSorted(ByVal theArrayList As ArrayList, _
                                  ByVal stringToInsert As String) As Boolean

    With theArrayList
        If CInt(stringToInsert) < CInt(.Item(.Count - 1)) Then Return False
        If .Count <= 1 Then
            If stringToInsert = "1" Then Return True Else Return False
        End If
        If .Item(.Count - 1).ToString = .Item(.Count - 2).ToString Then
            Return True
        Else
            Return False
        End If
    End With

End Function

If your ArrayList is NOT ordered, but still contains strings of numbers (assuming you started with number "1",) then the following function should work:

Private Function OKToInsertUNSorted(ByVal theArrayList As ArrayList, _
                                    ByVal stringToInsert As String) As Boolean

    If stringToInsert = "1" Then Return True

    Dim stringToCheck As String = CStr(CInt(stringToInsert) - 1)

    Dim qry = From stringItem In theArrayList _
              Where stringItem.ToString = stringToCheck _
              Group By stringItem Into _
              stringCount = Count()

    For Each result In qry
        If result.stringCount >= 2 Then Return True
    Next

    Return False

End Function

I put quite a bit of validation code in the first function based on some assumptions about what you were looking for, so your mileage may vary.

knslyr
If stringToInsert = "1" Then Return True ??? Why this statement. ArrayList can contains any number not '1' may be there or '1' may not.
James123
It was an assumption about the values you would be storing. "1" was assumed to be the first value in an ordered list, or the lowest value in a non-ordered list. If that isn't the case, feel free to take it out :)
knslyr
What types of values will you be storing in your arraylist? What limitations/validations are there? What range?
knslyr