473,569 Members | 2,879 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array Sort Algorithm to find duplicate values

25 New Member
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

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
10 4115
Killer42
8,435 Recognized Expert Expert
One tip - by using a Collection (or possibly a Dictionary) instead of an array, you can prevent duplicates being added.
Sep 6 '07 #2
Killer42
8,435 Recognized Expert Expert
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
cmdolcet69
25 New Member
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
Killer42
8,435 Recognized Expert Expert
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
1,977 Top Contributor
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
1,295 Recognized Expert Top Contributor
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
1,977 Top Contributor
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
180 New Member
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
1,977 Top Contributor
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

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

Similar topics

18
2456
by: Dan | last post by:
hello, I would to know if it is possible to delete an instance in an array, The following does not allow me to do a delete. I am trying to find and delete the duplicate in an array, thanks for ( j =0; j<MAX ; j++) { for ( i =0; i<MAX ; i++)
21
3179
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a...
5
28328
by: Paulers | last post by:
Hello all, I have a string array with duplicate elements. I need to create a new string array containing only the unique elements. Is there an easy way to do this? I have tried looping through each element but I am having issues using redim to adjust the new array. Any help or example code would be greatly appreciated. thanks!
11
2047
by: cdg | last post by:
Could anyone tell me how to write a loop for an array, that would have to check each iteration for duplications of previous entered values. So, the exact number of loops is not known, but the total number of array elments is known. Example: array size - array (some type of loop statement) { array "equal to" or "not equal to" a variable....
9
16934
by: Jae | last post by:
Hi I wonder how can I implement the STL map sorting by value. For example, I have a map m map<int, intm; m = 10; m = 5; m = 6;
23
7372
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion....
11
4161
by: holla | last post by:
Write the following functions that can be called by a C program: Write a function to populate an array with random integers between 0 and 99. Use the following functions of which the prototypes are to be found in stdlib.h • randomize() – use once to initialize the randomization process • rand() – generates a random number between 0 and the...
6
1732
by: Peter | last post by:
Hi I have a number of arrays of longs, from which I need to find a single array which only contains the values which appear in all the original arrays. For example, I could have the three arrays: 1, 3, 2, 8, 5 3, 6, 1
2
3740
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error when i do the following. if you add a contact with up to 13 fields it will be stored in a data structure. i have a tabbed pane that will show six of...
0
7930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7681
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6290
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.