|
|
#1 |
|
Banned
Join Date: Jan 01
Location: Dublin, Ireland
Posts: 10,359
![]() |
Removing duplicates from an array
How to remove duplicated items in an array.
This is (in my opinion) a very fast way of doing it. I don't know of any faster methods. If you need to remove duplicated items from an array of a different type, then just adjust the code accordingly. Note: You will need to add a reference to "Microsoft Scripting Runtime" as the code uses its Dictionary object. To do this, select Project from the toolbar, then select "References", and then select "Microsoft Scripting Runtime" VB Code:
Last edited by plenderj; Apr 2nd, 2002 at 04:24 AM. |
|
|
|
|
|
#2 |
|
Banned
Join Date: Jan 01
Location: Dublin, Ireland
Posts: 10,359
![]() |
I still believe this to be the fastest method of removing duplicated items available in Classic VB.
|
|
|
|
|
|
#3 |
|
Banned
Join Date: Jan 01
Location: Dublin, Ireland
Posts: 10,359
![]() |
* 21-October-2004 - Moved to CodeBank *
|
|
|
|
|
|
#4 |
|
Junior Member
Join Date: Oct 04
Location: world
Posts: 19
![]() |
how to use it in text please
|
|
|
|
|
|
#5 |
|
Banned
Join Date: Jan 01
Location: Dublin, Ireland
Posts: 10,359
![]() |
Just modify the code above to filter out Strings instead of Longs.
|
|
|
|
|
|
#6 | |
|
VB6, XHTML & CSS hobbyist
Join Date: Oct 02
Location: Finland
Posts: 6,330
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Re: Removing duplicates from an array
Quote:
![]() This one is faster with strings by 10 - 20% VB Code:
This works for Byte, Integer and Long datatypes and is four to five times faster: VB Code:
To convert it to use Long for example, just use VB's inbuilt replace from the edit menu and make it convert Byte to Long. And rename the function, of course ![]() What is the best thing with these functions: you don't need to add any extra reference to your project!
__________________
Unicode classes, functions... in Visual Basic 6
VB6 in occasional use. I'm mostly HTML, CSS & JavaScript these days. « Antec Sonata II: Core 2 Duo E7400, ASRock P45TS, Asus EN9600GT 512 MB, 4 GB, 1.25 TB » « OS: Windows 7 | Laptop: Amilo Pi 2530-12P| Netbook: Asus EEE 901 » Last edited by Merri; Dec 30th, 2004 at 10:56 PM. |
|
|
|
|
|
|
#7 |
|
Banned
Join Date: Jan 01
Location: Dublin, Ireland
Posts: 10,359
![]() |
Re: Removing duplicates from an array
Can you post the code you used to compare the times, because in the brief test I did my code still worked faster...
|
|
|
|
|
|
#8 |
|
Hyperactive Member
Join Date: Feb 03
Location: Greeneville, TN
Posts: 274
![]() |
Re: Removing duplicates from an array
__________________
Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde My little blog: http://vblore.blogspot.com/ |
|
|
|
|
|
#9 |
|
Banned
Join Date: Jan 01
Location: Dublin, Ireland
Posts: 10,359
![]() |
Re: Removing duplicates from an array
Ah I take it you're comparing my code to your ASM code?
|
|
|
|
|
|
#10 | |
|
Hyperactive Member
Join Date: Feb 03
Location: Greeneville, TN
Posts: 274
![]() |
Re: Removing duplicates from an array
Quote:
__________________
Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde My little blog: http://vblore.blogspot.com/ |
|
|
|
|
|
|
#11 |
|
New Member
Join Date: Nov 09
Posts: 1
![]() |
Re: Removing duplicates from an array
How about this compared to your methods? How are you comparing the speeds?
Function removeDuplicates(ByVal initialArray As String()) As String() Dim i As Integer = 0 Dim j As Integer = 0 Dim newArray(0) As String For i = 0 To UBound(initialArray) For j = 0 To UBound(initialArray) If Not initialArray(i) = "" Then If Not j = i Then If initialArray(i) = initialArray(j) Then initialArray(j) = "" End If End If End If Next Next j = 0 For i = 0 To UBound(initialArray) If Not initialArray(i) = "" Then ReDim Preserve newArray(j) newArray(j) = initialArray(i) j = j + 1 End If Next Return newArray End Function |
|
|
|
![]() |
|
||||||
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|