Click to See Complete Forum and Search --> : RemoveDuplicateValues from Array


thomas_dipu
March 27th, 2001, 09:47 PM
Hi There,

U might find this silly, I am stuck with one thing.WEll I have this
problem I have this array a[n] = [1,2,2,1,3,3,4,4,5,6]
I just want to remove the duplicates and get the o/p as [1,2,3,4,5,6],
this is to be stored in an array X[]
What I tried is like this
for i = 0 to n
for j = i + 1 to n
if a[i] != a[j] then
x[j] = a[i]
end if
next j
next i
The problem with the above is that it will print the dissimilar numbers
more than once like 1,2,1 etc etc.

NOTE - PLS READ BELOW ALSO

The following is the exact scenario, I am using VB to compare, but the logic should be same, so I welcome any suggestions

I am already sorting the o/p, thru my SQL. This is the O/p from the SQL.

ID Comments
5 This is Test5a <--
5 This is Test5b
5 This is Test5c
5 This is Test 5b
1 This is Test 1a <--
1 This is Test 1b
1 This is Test 1c

From the above O/P I should be able to filter the arrow ones into an array.

The o/p should show only the arrow ones , Meaning I only have to compare for the ID, If it is not equal just display the first one.

O/p
5 This is Test5a
1 This is Test1a

I would really appreciate if somebody is able to help

Thanks
Dipu Thomas

phil m
March 27th, 2001, 11:33 PM
See code below

with command1 we use code

with command2 we use a listbox with its SORTED property set to true.

command2 might be better if you have stacks of information coming in from a variety of places or if the array is very long as there is less looping to worry about.

Phil


Option Explicit

Private Sub Command1_Click()
Dim a(5) As Integer
Dim b(5) As String
Dim x As Integer
Dim y As Integer
Dim c As Integer
Dim flag As Boolean


a(0) = 3
a(1) = 2
a(2) = 3
a(3) = 4
a(4) = 2
a(5) = 6

For x = 0 To UBound(a)
For y = x + 1 To UBound(a)

If a(x) = a(y) Then
flag = True
Exit For
End If

Next y

If flag = False Then
b(c) = CInt(a(x))
c = c + 1
flag = False

End If

flag = False

Next x

For x = 0 To UBound(b)
If IsNumeric(b(x)) Then Print b(x)

Next x

End Sub

Private Sub Command2_Click()

Dim a(5) As Integer
Dim b(5) As String
Dim x As Integer

a(0) = 3
a(1) = 2
a(2) = 3
a(3) = 4
a(4) = 2
a(5) = 6

For x = 0 To UBound(a)
List1.AddItem a(x)
Next x

For x = 1 To List1.ListCount
If List1.List(x) <> List1.List(x - 1) Then b(x - 1) = List1.List(x - 1)
Next x

For x = 0 To UBound(b)
If IsNumeric(b(x)) Then Print b(x)
Next x

List1.Clear

End Sub

Iouri
March 28th, 2001, 07:17 AM
' Filter out duplicate values in an array and compact
' the array by moving items to "fill the gaps".
' Returns the number of duplicate values
'
' it works with arrays of any type, except objects
'
' The array is not REDIMed, but you can do it easily using
' the following code:
' a() is a string array
' dups = FilterDuplicates(a())
' If dups Then
' ReDim Preserve a(LBound(a) To UBound(a) - dups) As String
' End If

Function FilterDuplicates(arr As Variant) As Long
Dim col As Collection, index As Long, dups As Long
Set col = New Collection

On Error Resume Next

For index = LBound(arr) To UBound(arr)
' build the key using the array element
' an error occurs if the key already exists
col.Add 0, CStr(arr(index))
If Err Then
' we've found a duplicate
dups = dups + 1
Err.Clear
End If
' if we've found one or more duplicates so far
' we need to move elements towards lower indices
If dups Then
arr(index - dups) = arr(index)
arr(index) = Empty
End If
Next

' return the number of duplicates
FilterDuplicates = dups

End Function



Iouri Boutchkine
iouri@hotsheet.com

thomas_dipu
March 29th, 2001, 08:47 AM
Thank You Sir, I used your code only.

I really appreciate ur help

thomas_dipu
March 29th, 2001, 08:50 AM
I really appreciate your help.

Thanks a Loooot
Dipu Thomas

Iouri
March 29th, 2001, 09:52 AM
Rate it if it helped you

Iouri Boutchkine
iouri@hotsheet.com