By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,731 Members | 1,129 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,731 IT Pros & Developers. It's quick & easy.

how to find duplicate values in a array

chandru8
100+
P: 145
hi to all
iam using vb6.0 can any one tell me how to duplicate values in a array if possible i want to delete the duplicate values
its urgent
thanks
Jan 8 '08 #1
Share this Question
Share on Google+
10 Replies


kadghar
Expert 100+
P: 1,295
hi to all
iam using vb6.0 can any one tell me how to duplicate values in a array if possible i want to delete the duplicate values
its urgent
thanks
yes, there are many ways and methods, some faster than others. The worst, slower, but easier will be to check each one vs each one, lets say your array is Arr1

Expand|Select|Wrap|Line Numbers
  1. dim i, j, n as integer
  2. dim list() as integer 
  3. n=0
  4. for i = lbound(arr1) to ubound(arr1) - 1
  5.     for j = i + 1 to ubound(arr1)
  6.         if arr1(i) = arr1(j) then 
  7.             'here use "home made function" to delete the j-th item from the array
  8.             j=j-1
  9.         end if
  10.     next j
  11. next i
well, that wont be fast, but it'll work.

HTH

Edit: ok, i have some free time:
home made function:
Expand|Select|Wrap|Line Numbers
  1. arr1(j) = arr1(ubound(arr1))
  2. redim preserve arr1(lbound(arr1) to ubound(arr1) -1)
^.^ wasnt that hard
Jan 9 '08 #2

Expert 5K+
P: 8,434
I don't think you'll find many faster methods. The mistake a lot of people make is starting the inner loop form the beginning each time.

Note also, your home-made function will work only if the sequence of the elements is not significant. Oh, and you may find that a bit of extra testing or error-handling is required, since your code may try to go past the end of the array.

However, what about an alternative? If you use a collection rather than an array, you can easily prevent any duplicates being added in the first place.

There are a couple of minor tweaks which might make some difference. I'll list them in descending order of possible performance impact...
  • Make sure the "home-made function" is coded in-line rather than as an actual function, to reduce time used up in calling it.
  • Keep track of the upper bound and just do one ReDim Preserve at the end, rather than each time you remove an entry.
  • Change all Integer variables to Long.
  • Define i and j as Integer or Long. You currently have them defined as the default type, most likely Variant. (No, check your syntax - they are not defined as Integer. Only n is.)
  • Remove variable names from the Next statements. (This comes from an older version of BASIC and may not relate to VB, but you don't need them anyway.)
And a couple of general comments...
  • What the heck is list()?
  • You do not need to set n = 0 at the start. It's only just been created.
  • Actually, you don't even need to define n, since it isn't used.
Jan 9 '08 #3

QVeen72
Expert 100+
P: 1,445
Hi,

One of the Fastest way would be, if using a Database, add all the Values of Array to a TempTable. And Query Distinct Field Values..

Regards
Veena
Jan 9 '08 #4

Expert 5K+
P: 8,434
One of the Fastest way would be, if using a Database, add all the Values of Array to a TempTable. And Query Distinct Field Values.
Surely a collection (or even looping through the array) would have to be faster than using a database?
Jan 9 '08 #5

QVeen72
Expert 100+
P: 1,445
Surely a collection (or even looping through the array) would have to be faster than using a database?
Hi,

I actually meant "Least Coding" or "Simple Logic"


Regards
Veena
Jan 9 '08 #6

chandru8
100+
P: 145
thanks for your reply

iam having another doubt
am comparing two arrays, in array1 the size is 100 and for array2 it will took the count of first array
but my problem is
i saved 5 values to array1 ,rest of the values will be null

while comparing i need only the 5 values from array1 but it shows it has 100 values with nulls

we can restrict the values in the array1 by checking null, is there any alternative to have only the fivwe values.


can you please give me any example for multi dimensinal array.
Jan 9 '08 #7

QVeen72
Expert 100+
P: 1,445
Hi,

Declare the Array as Variant:

Dim MyArr(1 To 100)

And Check with Function IsEmpty: If you have not assigned any value to the array item, then it will be Empty..

If IsEmpty(MyArr(6)) Then


Regards
Veena
Jan 9 '08 #8

kadghar
Expert 100+
P: 1,295
...
Note also, your home-made function will work only if the sequence of the elements is not significant. Oh, and you may find that a bit of extra testing or error-handling is required, since your code may try to go past the end of the array.

And a couple of general comments...
  • What the heck is list()?
  • You do not need to set n = 0 at the start. It's only just been created.
  • Actually, you don't even need to define n, since it isn't used.
Well about the general comments, sorry, i took it from some other code i had and forgot to clean it.
And since you're using REDIM each time you're avoiding the error handler, since you wont exceed the limits, that're actually redefined each time you remove an item, but using REDIM and UBOUND each time coud slow down things a little bit. Well, as you said, it could be faster using a counter and an error handler, or a collection.
Jan 9 '08 #9

Expert 5K+
P: 8,434
And since you're using REDIM each time you're avoiding the error handler, since you wont exceed the limits, that're actually redefined each time you remove an item...
Actually, that's not quite correct. It's because of the ReDim that you may overrun the array. Your loop will continue on to where the array originally ended, but by then it may have shrunk.

The ending point of a FOR loop is set at the start, and does not change. So although the value returned by the Ubound() function may have changed, the FOR loop will still keep going.
Jan 10 '08 #10

Expert 5K+
P: 8,434
...Check with Function IsEmpty...
If possible (it depends on your circumstances), it would be more efficient to keep track of how many you've populated, and only access that many.
Jan 10 '08 #11

Post your reply

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