Results 1 to 7 of 7

Thread: array duplicate question - tricky

  1. #1

    Thread Starter
    Addicted Member killer_cobra's Avatar
    Join Date
    Jun 2001
    Location
    Lost in space
    Posts
    131

    Unhappy array duplicate question - tricky

    Hi,
    I have a problem with finding the number that has the most duplicates within an array containing several thousand entries...

    I can sort through and find dupes no problem, can find Min/Max and Average, but how do I go about finding the number that is duplicated the most within this array?

    Small Example:

    1234, 3323, 4567, 1234, 3323, 3444, 4444, 3323, 6678

    I don't know what the real values will be when placed into the array, but in this example, 3323 is duplicated more times than any other number... I need to extract the number that has the most duplicates when looping through the array... Any ideas?
    Does not matter how, or when extraction occurs...

    Thanx a million in advance,

    -Scott
    Error... What Error?!?! I've only typed a single character!
    ______________________________
    VB 6.0 EE, Win 2k Pro on AMD Duron 1Ghz/ 384 MB SDRAM

  2. #2
    Addicted Member
    Join Date
    Nov 2002
    Posts
    155
    I guess you could try something like this. Have another array which will contain the count for each number. Every time you encounter a new number in the first array that is largest so far, redim the second array to the value of the number from the first array, and put the value '1' into that position in the second array. If the number is smaller than largest so far, then you don't need to redim; the position already exists in the second array so just put the '1' straight in.

    Then when you encounter a number for the second or subsequent time in the first array, increment its count in the second one.

    Ok, one problem..... if the numbers in the first array are very big (or even if only one number is) then of course the ubound of the second array will be huge too.

  3. #3
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611
    Or make a second array 2 dimensional, count how many times the first number occours then write that number and the number of times in the array

    array(1,1) = 1234
    array(1,2) = 1
    array(2,1) = 3233
    array(2,2) = 2

    Then find the highest number, from the array(x,2) range, and read then array(x,1) value, that is which number occurs most times

  4. #4
    Addicted Member
    Join Date
    Nov 2002
    Posts
    155
    Only prob with that tho' Lightning, is that every time you encounter a number in the first array you have to find it in your second array before you can increment its count, and add it if it's not there yet.

    So your way is process intensive, mine uses memory. So he'll have to make a call on which suits best.

  5. #5
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611
    You can easily check wether the number you are going to count is already in the array, if don't check and go on to the next number, proberbly uses less memory as a array (6678) + . But it costs more processortime, I agree, he should deside



    edit

    Sorry misread your post I said nothing, completly nothing.....

  6. #6
    Lively Member
    Join Date
    Feb 2002
    Posts
    102
    I wrote an algorithm last year based on something similar. It uses an UDT as follows:

    Code:
    Type WordInformation
    word as string
    count as int
    end type
    
    '*****************************************************************
    'This proc count the duplicates held in the array and then
    'increments the count of the first word that it is comparing.
    'So if there is a duplicate, then the count is incremented and
    'the element containig the duplicate is replaced with ""
    '*****************************************************************
    Dim i%, j%, highcount%
        highcount = 1
        For i = 1 To UBound(Words) - 1
            For j = i + 1 To UBound(Words)
                If Words(i).word = Words(j).word Then
                    Words(i).count = Words(i).count + 1
                    if Words(i).count > highcount then highcount = Words(i).count
                    Words(j).word = ""
                End If
            Next j
        Next i
        i = 2
        For j = 2 To UBound(Words)
            If Words(j).word <> "" Then
                Words(i) = Words(j)
                i = i + 1
            End If
        Next j
        ReDim Preserve Words(1 To i - 1)
    where highcount is the most frequent word. You may not need thsecond part of the algorithm
    You would just need to modify the type statement at the top and I think it should work. ie change word to number and count to numbercount


    Hope this helps
    Last edited by darthy; Apr 15th, 2003 at 05:18 AM.

  7. #7
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: array duplicate question - tricky

    Originally posted by killer_cobra
    Hi,
    I have a problem with finding the number that has the most duplicates within an array containing several thousand entries...

    I can sort through and find dupes no problem, can find Min/Max and Average, but how do I go about finding the number that is duplicated the most within this array?

    Small Example:

    1234, 3323, 4567, 1234, 3323, 3444, 4444, 3323, 6678

    I don't know what the real values will be when placed into the array, but in this example, 3323 is duplicated more times than any other number... I need to extract the number that has the most duplicates when looping through the array... Any ideas?
    Does not matter how, or when extraction occurs...

    Thanx a million in advance,

    -Scott
    Try this ... it will give you ideas on some creative uses of string functions to solve this sort of problem. Its kinda clunky and thrown together, but should give you the idea. Definitly cuts down on lines of code if your into that

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim a(9) As String
    3. Dim temp1 As Integer, temp2 As Integer
    4. Dim i As Integer
    5.  
    6. a(1) = 1234
    7. a(2) = 3323
    8. a(3) = 4567
    9. a(4) = 1234
    10. a(5) = 3323
    11. a(6) = 3444
    12. a(7) = 4444
    13. a(8) = 3323
    14. a(9) = 6678
    15.  
    16. temp1 = 0
    17. For i = 1 To UBound(a)
    18.     If UBound(a) - UBound(Split(Replace(Join(a, "-"), a(i) & "-", ""), "-")) > temp1 Then
    19.         temp1 = UBound(a) - UBound(Split(Replace(Join(a, "-"), a(i) & "-", ""), "-"))
    20.         temp2 = i
    21.     End If
    22. Next i
    23. MsgBox a(temp2) & " repeated " & temp1 & " times."
    24.  
    25. End Sub

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