By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,101 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
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
  1. Public Sub SortArraylist()
  2.  
  3.         For Each intarrayindex In _ReadingArrayList3
  4.             If _ReadingArrayList3.Then Then
  5.  
  6.             Else
  7.             End If
  8.         Next
  9.     End Sub
Sep 6 '07 #1
Share this Question
Share on Google+
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
  1. Public Function IsArrayEntryDuplicated(TheArray As Variant, WhichEntry As Long) As Boolean
  2.   Dim I As Long
  3.   For I = LBound(TheArray) to UBound(TheArray)
  4.     If TheArray(I) = TheArray(WhichEntry) Then
  5.       If I <> WhichEntry Then
  6.         IsArrayEntryDuplicated = True
  7.         Exit For
  8.       End If
  9.     End If
  10.   Next
  11. 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
  1. Public Function IsArrayEntryDuplicated(TheArray As Variant, WhichEntry As Long) As Boolean
  2.   Dim I As Long
  3.   For I = LBound(TheArray) to UBound(TheArray)
  4.     If TheArray(I) = TheArray(WhichEntry) Then
  5.       If I <> WhichEntry Then
  6.         IsArrayEntryDuplicated = True
  7.         Exit For
  8.       End If
  9.     End If
  10.   Next
  11. 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

hariharanmca
100+
P: 1,977
Expand|Select|Wrap|Line Numbers
  1. Private Sub CheckArrayDuplicated(chkArray As Variant, Optional chkString As Variant, Optional lngChecked As Long)
  2.     If lngChecked <= 0 Then GoTo ReCallME
  3.     For i = LBound(MYARRAY) To UBound(chkArray)
  4.         If chkArray(i) = chkString And i <> lngChecked Then
  5.             MsgBox chkString & " - Duplicated in Array list"
  6.             GoTo ExitMe
  7.         End If
  8.     Next i
  9. ReCallME:
  10.     lngChecked = lngChecked + 1
  11.     If lngChecked < UBound(chkArray) Then chkString = chkArray(lngChecked) Else: GoTo ExitMe
  12.     CheckArrayDuplicated chkArray, chkString, lngChecked
  13. ExitMe:
  14. End Sub
you can call this method like
Expand|Select|Wrap|Line Numbers
  1. CheckArrayDuplicated MYARRAY
In this method, No need to sort.
Sep 6 '07 #6

kadghar
Expert 100+
P: 1,295
Expand|Select|Wrap|Line Numbers
  1. Private Sub CheckArrayDuplicated(chkArray As Variant, Optional chkString As Variant, Optional lngChecked As Long)
  2.     If lngChecked <= 0 Then GoTo ReCallME
  3.     For i = LBound(MYARRAY) To UBound(chkArray)
  4.         If chkArray(i) = chkString And i <> lngChecked Then
  5.             MsgBox chkString & " - Duplicated in Array list"
  6.             GoTo ExitMe
  7.         End If
  8.     Next i
  9. ReCallME:
  10.     lngChecked = lngChecked + 1
  11.     If lngChecked < UBound(chkArray) Then chkString = chkArray(lngChecked) Else: GoTo ExitMe
  12.     CheckArrayDuplicated chkArray, chkString, lngChecked
  13. ExitMe:
  14. End Sub
you can call this method like
Expand|Select|Wrap|Line Numbers
  1. 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

hariharanmca
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
  1. If lngChecked <= 0 Then GoTo ReCallME
  2. .............
  3. ................
  4. ........
  5. ReCallME:
  6.     lngChecked = lngChecked + 1
  7. ...........
  8. ....
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

Robbie
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

hariharanmca
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

waynespangler
P: 8
As someone said up the line, use a collection. As in:
Expand|Select|Wrap|Line Numbers
  1.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         Dim myAry() As Integer = {3, 6, 4, 7, 5, 5, 3, 8, 9, 1, 3}
  3.         Dim myCollection As New Collection
  4.         For x As Integer = 0 To UBound(myAry)
  5.             Try
  6.                 myCollection.Add(myAry(x), myAry(x))
  7.             Catch
  8.             End Try
  9.         Next
  10.         ReDim myAry(myCollection.Count)
  11.         For x As Integer = 1 To myCollection.Count
  12.             myAry(x) = myCollection.Item(x)
  13.         Next
  14.     End Sub
  15.  
Sep 7 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.