429,101 Members | 1,340 Online
Need help? Post your question and get tips & solutions from a community of 429,101 IT Pros & Developers. It's quick & easy.

# Array Sort Algorithm to find duplicate values

 P: 25 i need to find a way to look through my _ReadingArrayList3 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 _ReadingArrayList3, 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 Expand|Select|Wrap|Line Numbers Public Sub SortArraylist()           For Each intarrayindex In _ReadingArrayList3             If _ReadingArrayList3.Then Then               Else             End If         Next     End Sub Sep 6 '07 #1
10 Replies

 Expert 5K+ P: 8,434 One tip - by using a Collection (or possibly a Dictionary) instead of an array, you can prevent duplicates being added. Sep 6 '07 #2

 Expert 5K+ P: 8,434 Here's a little routine that might be handy... Expand|Select|Wrap|Line Numbers Public Function IsArrayEntryDuplicated(TheArray As Variant, WhichEntry As Long) As Boolean   Dim I As Long   For I = LBound(TheArray) to UBound(TheArray)     If TheArray(I) = TheArray(WhichEntry) Then       If I <> WhichEntry Then         IsArrayEntryDuplicated = True         Exit For       End If     End If   Next End Function 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. Sep 6 '07 #3

 P: 25 Here's a little routine that might be handy... Expand|Select|Wrap|Line Numbers Public Function IsArrayEntryDuplicated(TheArray As Variant, WhichEntry As Long) As Boolean   Dim I As Long   For I = LBound(TheArray) to UBound(TheArray)     If TheArray(I) = TheArray(WhichEntry) Then       If I <> WhichEntry Then         IsArrayEntryDuplicated = True         Exit For       End If     End If   Next End Function 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 Sep 6 '07 #4

 Expert 5K+ P: 8,434 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? Sep 6 '07 #5

 100+ P: 1,977 Expand|Select|Wrap|Line Numbers Private Sub CheckArrayDuplicated(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(lngChecked) Else: GoTo ExitMe     CheckArrayDuplicated chkArray, chkString, lngChecked ExitMe: End Sub you can call this method like Expand|Select|Wrap|Line Numbers CheckArrayDuplicated MYARRAY In this method, No need to sort. Sep 6 '07 #6

 Expert 100+ P: 1,295 Expand|Select|Wrap|Line Numbers Private Sub CheckArrayDuplicated(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(lngChecked) Else: GoTo ExitMe     CheckArrayDuplicated chkArray, chkString, lngChecked ExitMe: End Sub you can call this method like Expand|Select|Wrap|Line Numbers CheckArrayDuplicated MYARRAY 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 Sep 6 '07 #7

 100+ P: 1,977 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 Expand|Select|Wrap|Line Numbers If lngChecked <= 0 Then GoTo ReCallME ............. ................ ........ ReCallME:     lngChecked = lngChecked + 1 ........... .... Iyes this function will check twice. It's just a concept! not an exact Algorithm We can check once using another tmpArray. Sep 7 '07 #8

 100+ P: 180 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) Sep 7 '07 #9

 100+ P: 1,977 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. Sep 7 '07 #10

 P: 8 As someone said up the line, use a collection. As in: Expand|Select|Wrap|Line Numbers     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   Sep 7 '07 #11