CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2001
    Posts
    9

    removing array duplicates

    how would i go about removing duplicates from an array without outputting it to a database and using SQL?


  2. #2

    Re: removing array duplicates

    I think U must parse your array.
    If U wnat to avoid the duplicate U can use Collection and add the Alphanumeric key for each elemenet

    hi,brt

    <center>
    <HR width=80%>
    <img src='http://web.tiscali.it/bertaplanet/im...ertaplanet.gif'>
    </center>

  3. #3
    Join Date
    Aug 2000
    Location
    England
    Posts
    185

    Re: removing array duplicates

    Hello,

    The way I would do it would be to sort the array into alphabetical or numerical order.

    Once you have done that then you then find duplicates by doing the following in VB



    loop
    get the current array item
    loop until no duplicate item found
    remove the duplicate item
    end loop
    increment the counter
    end





    Andrew


  4. #4

    Re: removing array duplicates

    yes, but for remove item U must use 2 array.
    something like that...

    dim temp_Array(ubound(myarray))
    dim i as long,J as long,Z as long
    dim flgDuplicated as boolean
    dim myItem

    z=0
    for i=1 to ubound(myarray)
    myItem=myArray(i)
    flgDuplicated=false
    for J=1 to ubound(myarray)
    if J<>i then
    if myItem=myarray(i) then
    flgDuplicated=true
    exit for
    end if
    end if
    next J
    if not flgDuplicated then
    z=z+1
    temp_array(z)=myitem
    end if
    next i

    redim myarray(z)

    for i=1 to z
    myarray(i)=temp_array(i)
    next i

    hi,brt

    <center>
    <HR width=80%>
    <img src='http://web.tiscali.it/bertaplanet/im...ertaplanet.gif'>
    </center>

  5. #5
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: removing array duplicates

    ' Filter out duplicate values in an array and compact
    ' the array by moving items to "fill the gaps".
    ' Returns the number of duplicate values
    '
    ' it works with arrays of any type, except objects
    '
    ' The array is not REDIMed, but you can do it easily using
    ' the following code:
    ' a() is a string array
    ' dups = FilterDuplicates(a())
    ' If dups Then
    ' ReDim Preserve a(LBound(a) To UBound(a) - dups) As String
    ' End If

    Function FilterDuplicates(arr As Variant) As Long
    Dim col As Collection, index As Long, dups As Long
    Set col = New Collection

    On Error Resume Next

    For index = LBound(arr) To UBound(arr)
    ' build the key using the array element
    ' an error occurs if the key already exists
    col.Add 0, CStr(arr(index))
    If Err Then
    ' we've found a duplicate
    dups = dups + 1
    Err.Clear
    End If
    ' if we've found one or more duplicates so far
    ' we need to move elements towards lower indices
    If dups Then
    arr(index - dups) = arr(index)
    arr(index) = Empty
    End If
    Next

    ' return the number of duplicates
    FilterDuplicates = dups

    End Function



    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  6. #6
    Join Date
    Aug 2000
    Location
    England
    Posts
    185

    Re: removing array duplicates

    Hello,

    The way I was considering would mean you could have a one-dimensional array and be able to delete items without having to resort to any form of indexing. For instance in the sorted list:

    array1 : A
    array2 : B
    array3 : B
    array4 : B
    array5 : B
    array6 : C
    array7 : C
    array8 : D
    array9 : E

    At the start the first loop would read the letter A, it would then enter the second loop and look for further instances of A. None would be found so the array would continue.

    The next item found would be B. The second loop would find another instance of B, so the remainder of the items in the array would be moved down to remove this. The array would now be as follows:

    A,B,B,B,C,C,D,E

    The second loop would continue until no match was found whereby there would be only one instance of B. The first loop would then continue for the next letter and so on.

    The final array would be A,B,C,D,E - with no duplicates.

    Andrew


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