473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to find duplicate values in a array

chandru8
145 100+
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
10 28020
kadghar
1,295 Expert 1GB
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
Killer42
8,435 Expert 8TB
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
1,445 Expert 1GB
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
Killer42
8,435 Expert 8TB
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
1,445 Expert 1GB
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
145 100+
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
1,445 Expert 1GB
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
1,295 Expert 1GB
...
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
Killer42
8,435 Expert 8TB
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
Killer42
8,435 Expert 8TB
...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

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

Similar topics

2
by: Trond | last post by:
I have been trying to fig out how to build up an array with dates. When its generated i want to be able to remove duplicate dates. The dates is generated by FileInfo object that is scanning a...
9
by: vbportal | last post by:
Hi, I would like to add BitArrays to an ArrayList and then remove any duplicates - can someone please help me forward. I seem to have (at leaset ;-) )2 problems/lack of understanding (see test...
1
by: JTreefrog | last post by:
Hello - I've read a ton of stuff about deleting duplicate values in an array. They are all very useful - they just haven't addressed an array of objects. Here's my array: var sDat = ; The...
12
by: joestevens232 | last post by:
Hello Im having problems figuring out how to remove the duplicate entries in an array...Write a program that accepts a sequence of integers (some of which may repeat) as input into an array. Write...
1
by: aknoch | last post by:
My basic situation is this - I ONLY want duplicates, so the opposite of DISTINCT: I have two tables. Ordinarily, Table1ColumnA corresponds in a one to one ratio with Table2ColumnB through a...
10
by: cmdolcet69 | last post by:
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...
5
by: raviufor | last post by:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include <ctype.h> #include <ctype.h> #include <time.h> #include <sys/types.h>
2
by: raphael001 | last post by:
In my Visual Basic program I'm just trying to find duplicate values entered into an array from an inputbox, but i can't seem to get the coding right on the final part to check for duplicate values...
2
by: dawn123 | last post by:
I have a 2-d array that the user enters tress species and diameter so it looks something like: sm 10 rm 18 bf 12 sm10 sm10 rm18 and so on.. I need to be able to seach though my array and find...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.