Results 1 to 9 of 9

Thread: Array

  1. #1

    Thread Starter
    Lively Member James Bond 007's Avatar
    Join Date
    May 2000
    Location
    London
    Posts
    116
    Array(0) = "A"
    Array(1) = "B"
    Array(2) = "C"
    Array(3) = "D"
    Array(4) = "A"


    What is the fastest and shortest codes to find the duplicates in an array?






    Unrelated, how do you put the thumb icon in the body of my posting. I know how to put faces.
    License to Program

    007

  2. #2
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Arrow

    Not extremely knowledgable on arrays, but couldn't you look for the item in the array before adding the next item to the array?
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    I don't know the answer to your first question, but there have been a number of posts asking how to remove duplicates so if you do a search you may find the answer. But if you can use a collection instead of an array, then you can check for a duplicate key with only 2 or 3 lines of code and I'm sure that the collection approach is faster than any approach using an array (unless there is some API that you can use).

  4. #4
    Guest
    You can loop through them.

    Code:
    Function CheckArray(vValue As Variant, vArray) As Boolean
        For I = LBound(vArray) To UBound(vArray)
            If vArray(I) = vValue Then CheckArray = True
        Next I
    End Function
    And you would use it like:
    Code:
    Dim MyArray(50) As Byte
    MyArray(1) = 3
    
    If CheckArray(3, MyArray()) = True Then
        'Already in array
        Print "Already in array"
    Else
        'Not in array
        Print "Not in Array"
    End If

  5. #5

    Thread Starter
    Lively Member James Bond 007's Avatar
    Join Date
    May 2000
    Location
    London
    Posts
    116
    No! I can't do it like that for other reasons.

    Right now, I am using a For or Do loop and have it loop through.

    I don't want to do it like that because if my array goes up to 100, that means it has to do 10,000 comparison.

    License to Program

    007

  6. #6

    Thread Starter
    Lively Member James Bond 007's Avatar
    Join Date
    May 2000
    Location
    London
    Posts
    116
    The last post was for James.
    License to Program

    007

  7. #7

    Thread Starter
    Lively Member James Bond 007's Avatar
    Join Date
    May 2000
    Location
    London
    Posts
    116
    Megatron, the loop that you made only checks for a given value. I am looking for something that gives the entire array and it will returns only the duplicate ones. Thank you Megatron.

    Martin, what is the difference between an array and a collection. I thought it was use in a similar method.
    License to Program

    007

  8. #8
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'this is very Mickey but it works
    'someone can fix it to compare 1st and last
    
    Sub MyCompare(iArray As Variant)
         Dim myString
         Dim LoopOne As Long
         Dim LoopTwo As Long
         Dim x As Integer, y As Integer
         x = LBound(iArray)
         y = UBound(iArray)
    'for some reason I'm not checking the 1st against the last
    'so I improvise and use it this way
    
      If iArray(x) = iArray(y) Then
              dup = True
              myString = iArray(x)
       End If
       
    'check the array
         For LoopOne = UBound(iArray) To LBound(iArray) Step -1
           For LoopTwo = LBound(iArray) + 1 To LoopOne
             
             If iArray(LoopTwo - 1) = iArray(LoopTwo) Then
                myString = iArray(LoopTwo)
                dup = True
             End If
                          
           Next LoopTwo
         Next LoopOne
         
         If dup = True Then MsgBox "Duplicate " & myString
         
       End Sub
    
     
    Private Sub Command1_Click()
        Dim myArray(4) As Variant
        myArray(0) = "A"
        myArray(1) = "A"
        myArray(2) = "C"
        myArray(3) = "A"
        myArray(4) = "W"
        
        Call MyCompare(myArray)
    
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  9. #9
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    If you're putting a loop in a loop then you are (as was said above) looping Ubound(Array) ^ 2 times.

    It would be quicker to sort the array using a quicksort (or a bubble sort if the array was usually mostly sorted, or a counting sort if you were only dealing with numbers) then loop though the array once checking if Array(x) = Array(x+1)

    I'm sure that if you do a search on this site for Quicksort you'll get several examples of the algorithm.

    That'd be quicker than using a collection I think
    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

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