Array Sort Algorithm to find duplicate values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cmdolcet69
    New Member
    • Sep 2007
    • 25

    Array Sort Algorithm to find duplicate values

    i need to find a way to look through my _ReadingArrayLi st3 and see if any number stored in the inarrayindex are duplicated. I need to first collect all the data and place it into the array, once the array is filled i call my sort SortArray() Sub that will look at every arrayindex in the _ReadingArrayLi st3, at this time after that i can;t seem to find out how to compare values in the array. Below is the code i have been working on.

    Thanks

    [CODE=vb]Public Sub SortArraylist()

    For Each intarrayindex In _ReadingArrayLi st3
    If _ReadingArrayLi st3.Then Then

    Else
    End If
    Next
    End Sub[/CODE]
    Last edited by Killer42; Sep 6 '07, 12:31 PM. Reason: Added [CODE=vb] tag
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    One tip - by using a Collection (or possibly a Dictionary) instead of an array, you can prevent duplicates being added.

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Here's a little routine that might be handy...

      [CODE=vb]Public Function IsArrayEntryDup licated(TheArra y As Variant, WhichEntry As Long) As Boolean
      Dim I As Long
      For I = LBound(TheArray ) to UBound(TheArray )
      If TheArray(I) = TheArray(WhichE ntry) Then
      If I <> WhichEntry Then
      IsArrayEntryDup licated = True
      Exit For
      End If
      End If
      Next
      End Function[/CODE]My syntax is probably not quite right, but hopefully you get the idea. Also, how well it works might depend on what version of VB you're using.

      Comment

      • cmdolcet69
        New Member
        • Sep 2007
        • 25

        #4
        Originally posted by Killer42
        Here's a little routine that might be handy...

        [CODE=vb]Public Function IsArrayEntryDup licated(TheArra y As Variant, WhichEntry As Long) As Boolean
        Dim I As Long
        For I = LBound(TheArray ) to UBound(TheArray )
        If TheArray(I) = TheArray(WhichE ntry) Then
        If I <> WhichEntry Then
        IsArrayEntryDup licated = True
        Exit For
        End If
        End If
        Next
        End Function[/CODE]My syntax is probably not quite right, but hopefully you get the idea. Also, how well it works might depend on what version of VB you're using.

        Killer42. Now im completely lost is there any way you can take the existing code in my message and work what your trying to do in there?

        Thanks, im very new to .net

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          You would place a reference to this function in line 4 of your code, I think.

          The problem is, if you are doing For Each, then how do you know which entry you are looking at? The index, I mean. And if you don't know which one you're looking at, then how do you know whether any duplicate you find is actually the same entry?

          Comment

          • hariharanmca
            Top Contributor
            • Dec 2006
            • 1977

            #6
            [CODE=vb]Private Sub CheckArrayDupli cated(chkArray As Variant, Optional chkString As Variant, Optional lngChecked As Long)
            If lngChecked <= 0 Then GoTo ReCallME
            For i = LBound(MYARRAY) To UBound(chkArray )
            If chkArray(i) = chkString And i <> lngChecked Then
            MsgBox chkString & " - Duplicated in Array list"
            GoTo ExitMe
            End If
            Next i
            ReCallME:
            lngChecked = lngChecked + 1
            If lngChecked < UBound(chkArray ) Then chkString = chkArray(lngChe cked) Else: GoTo ExitMe
            CheckArrayDupli cated chkArray, chkString, lngChecked
            ExitMe:
            End Sub[/CODE]

            you can call this method like
            [CODE=vb]CheckArrayDupli cated MYARRAY[/CODE]

            In this method, No need to sort.

            Comment

            • kadghar
              Recognized Expert Top Contributor
              • Apr 2007
              • 1302

              #7
              Originally posted by hariharanmca
              [CODE=vb]Private Sub CheckArrayDupli cated(chkArray As Variant, Optional chkString As Variant, Optional lngChecked As Long)
              If lngChecked <= 0 Then GoTo ReCallME
              For i = LBound(MYARRAY) To UBound(chkArray )
              If chkArray(i) = chkString And i <> lngChecked Then
              MsgBox chkString & " - Duplicated in Array list"
              GoTo ExitMe
              End If
              Next i
              ReCallME:
              lngChecked = lngChecked + 1
              If lngChecked < UBound(chkArray ) Then chkString = chkArray(lngChe cked) Else: GoTo ExitMe
              CheckArrayDupli cated chkArray, chkString, lngChecked
              ExitMe:
              End Sub[/CODE]

              you can call this method like
              [CODE=vb]CheckArrayDupli cated MYARRAY[/CODE]

              In this method, No need to sort.
              Hi Hari
              woulnt the For be from lbound(chkarray ) ¿?

              i think it'll work even better if you run it from lngchecked +1 to ubound(chkarray )
              ... no need to check twice and you can even get rid of the condition i <> longchecked

              Comment

              • hariharanmca
                Top Contributor
                • Dec 2006
                • 1977

                #8
                Originally posted by kadghar
                Hi Hari
                woulnt the For be from lbound(chkarray ) ¿?

                i think it'll work even better if you run it from lngchecked +1 to ubound(chkarray )
                ... no need to check twice and you can even get rid of the condition i <> longchecked
                I think that should be lngchecked -1 there. Because I already increase the value of lngchecked +1 in the first go to statement

                [CODE=vb]If lngChecked <= 0 Then GoTo ReCallME
                .............
                ............... .
                ........
                ReCallME:
                lngChecked = lngChecked + 1
                ...........
                ....[/CODE]

                Iyes this function will check twice. It's just a concept! not an exact Algorithm

                We can check once using another tmpArray.

                Comment

                • Robbie
                  New Member
                  • Mar 2007
                  • 180

                  #9
                  I'm not sure if someone's already mentioned this way of doing it, and I did try looking at peoples' code, but I found it hard to understand because there weren't comments in the code.

                  I've done this before, although the way I did it gets exponentially slower as it finds more values which aren't duplicates; in other words it's much faster between 0->50% done than 50%->100%.


                  You start with an array which you want to check for errors.
                  You make a new array. This will hold every value in the original array which has been seen so far which is not a duplicate.

                  You loop through every value in the original array. At each loop, we are testing to see if this index contains a duplicate value of any previous indeces we've looked at.

                  To do this, it compares the value in the original array at the current index, with every single value in the new array. If it matches, then there is a duplicate.

                  When it's done, and if there was never any moatch (so it's NOT a duplicate), we add the value in original array of the current index, to this new array. That way, the next time round, THIS line which we just checked will be included in the test for duplicates, if you see what I mean.
                  (This is why it gets slower - it has to keep checking against more and more values in the new array)

                  Comment

                  • hariharanmca
                    Top Contributor
                    • Dec 2006
                    • 1977

                    #10
                    Originally posted by Robbie
                    I'm not sure if someone's already mentioned this way of doing it, and I did try looking at peoples' code, but I found it hard to understand because there weren't comments in the code.
                    ............... ........
                    ............... .....
                    ...............
                    (This is why it gets slower - it has to keep checking against more and more values in the new array)
                    Let me explain some thing.
                    Let us welcome all the reply first then; we will research for the best and faster. We can get the best only by comparing solutions.
                    Just wait and see.

                    Comment

                    • waynespangler
                      New Member
                      • Sep 2006
                      • 8

                      #11
                      As someone said up the line, use a collection. As in:
                      Code:
                      	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                      		Dim myAry() As Integer = {3, 6, 4, 7, 5, 5, 3, 8, 9, 1, 3}
                      		Dim myCollection As New Collection
                      		For x As Integer = 0 To UBound(myAry)
                      			Try
                      				myCollection.Add(myAry(x), myAry(x))
                      			Catch
                      			End Try
                      		Next
                      		ReDim myAry(myCollection.Count)
                      		For x As Integer = 1 To myCollection.Count
                      			myAry(x) = myCollection.Item(x)
                      		Next
                      	End Sub

                      Comment

                      Working...