473,661 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to find duplicate values in a array

chandru8
145 New Member
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 28146
kadghar
1,295 Recognized Expert Top Contributor
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 Recognized Expert Expert
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 Recognized Expert Top Contributor
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 Recognized Expert Expert
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 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Top Contributor
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 Recognized Expert Top Contributor
...
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 Recognized Expert Expert
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

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

Similar topics

2
1478
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 folder for files. Here is some code for that. In this case i am only using a variable not an array. _info = new FileInfo(_fileName); base.Text = _info.LastWriteTime.ToShortDateString(); As i see it i must first generate the array with dates. Then...
9
5084
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 code below): (a)When adding BitArrays to the ArrayList and then looping through the ArrayList I seem to access only the latest added BitArray and I'm not exactly clear on best way to access each BItArray in the ArrayList (b)When I try to remove...
1
16599
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 array is produced from JSON sent to me from a database query. I can't do a "group by" in the sql query - i actually need the repeats in a different array. Anyway - I want to be able to remove duplicate sDat.sid entries in my sDat array - but I'm...
12
20788
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 a function that processes the array so that any duplicate values are eliminated. Write an output function that prints out the values of the array. You can assume that there are no more than 20 integers in the input. But there may be less. Zero...
1
2482
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 shared variable. So if I query TableB using the shared variable, there really should only be on record returned. In essence, if I run this and return TWO rows, it is very bad: select * from TableB where SharedVariable = 1234
10
4125
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 array is filled i call my sort SortArray() Sub that will look at every arrayindex in the _ReadingArrayList3, 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 ...
5
2346
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
2577
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 already entered. Any help would be greatly appreciated. This is what i have so far except the code to check for duplicate values: Dim intarray() As Integer Dim intindex As Integer Dim intcount As Integer Dim intsearch As Integer Dim intlow As...
2
3615
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 all the duplicate values and know the number of each dupliate value so sm 10 would be 3 and rm 18 would be 2 and so on.. until the end of the array. I then will take that array and seach for a value in another array.. ( i think i may be able to do...
0
8754
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8542
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7362
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4177
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1740
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.