CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2000
    Posts
    10

    duplicates in a lsit

    I haven't gotten an answer for this one yet. The user can enter a number of items in a list. How can I determine what item has been repeated the most? In the list
    1
    2
    3
    there would be none. However, in the list
    1
    1
    2
    3
    it would be 1. And in the list
    1
    1
    2
    2
    3
    it would be 1, 2. How could I do that?


  2. #2
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: duplicates in a lsit

    Try this, it works fastest with a Sorted List, but is still quick either way..
    private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (byval hwnd as Long, byval wMsg as Long, byval wParam as Long, lParam as Any) as Long
    private Const LB_FINDSTRINGEXACT = &H1A2

    private Sub Command1_Click()
    Dim iIndex as Long
    Dim iMatch as Long
    Dim iCopies as Long
    Dim iHighest as Long
    Dim aCommon() as Long
    Dim sString as string
    Dim bSkip as Boolean

    for iIndex = 0 to List1.ListCount - 1
    iCopies = 0
    iMatch = -1
    bSkip = false
    'Skip this one if it's the same as the last Item Checked
    If iIndex then bSkip = (List1.List(iIndex) = List1.List(iIndex - 1))
    'Skip this one if there's a previous instance of it in the List
    If Not bSkip then bSkip = (SendMessage(List1.hwnd, LB_FINDSTRINGEXACT, -1, byval List1.List(iIndex)) < iIndex)
    'While there are other Instances in the List..
    While iMatch <> iIndex And Not bSkip
    'Increment the No of Copies Found of this Item
    iCopies = iCopies + 1
    'Find the next Copy..
    iMatch = SendMessage(List1.hwnd, LB_FINDSTRINGEXACT, IIf(iMatch < 0, iIndex, iMatch), byval List1.List(iIndex))
    Wend
    'If there were more than 1 Copies
    If iCopies > 1 And Not bSkip then
    'If the No. of Copies is Greater or the Same as the Highest so far..
    If iCopies >= iHighest then
    If iCopies > iHighest then
    'new Highest Copies
    ReDim aCommon(0)
    else
    'Another Item with the same highest amount of Copies
    ReDim Preserve aCommon(UBound(aCommon) + 1)
    End If
    'Store this Index
    aCommon(UBound(aCommon)) = iIndex
    'Remember the Highest No. of Copies
    iHighest = iCopies
    End If
    End If
    next
    If iHighest then
    'If Copies were Found..
    for iIndex = 0 to UBound(aCommon)
    sString = sString & ", " & List1.List(aCommon(iIndex))
    next
    MsgBox "Most Repeated Item(s): " & vbCrLf & mid$(sString, 3) & _
    vbCrLf & vbCrLf & "Repeated " & iHighest & " Times.", _
    vbInformation + vbOKOnly, "Repeats"
    else
    'No Copies Found..
    MsgBox "No Items were Repeated", vbInformation + vbOKOnly, "No Repeats"
    End If
    End Sub



    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured