473,508 Members | 2,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

find the number of duplicate values in array

15 New Member
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 this second part) but its the finding dupliate values and number of them I'm havinig trouble with, I dont even know where to start. Thanks alot
Dec 5 '09 #1
2 3604
vb5prgrmr
305 Recognized Expert Contributor
From the sounds of it Dawn123, you will have to attack this with a brute force method of looping from beginning to end comparing strings, which in vb is kind of slow. This example as you can see is for a single dimension array and to modify it for a two dimension array just add another outer loop...
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Dim MyArray() As String
  4.  
  5. Private Sub Form_Load()
  6. MyArray = Split("sm10,rm18,bf12,sm10,sm10,rm18", ",")
  7. End Sub
  8.  
  9. Private Sub Command1_Click()
  10.  
  11. Dim StringToCount() As String
  12. Dim CountOfString() As Integer
  13. Dim OuterLoopCounter As Integer, InnerLoopCounter As Integer
  14. Dim UpperBoundsOfArray As Integer, LowerBoundsOfArray As Integer
  15. Dim UniqueStringCounter As Integer
  16. Dim MessageString As String
  17.  
  18. LowerBoundsOfArray = LBound(MyArray)
  19. UpperBoundsOfArray = UBound(MyArray)
  20.  
  21. For OuterLoopCounter = LowerBoundsOfArray To UpperBoundsOfArray
  22.  
  23.   If IsStringInArray(MyArray(OuterLoopCounter), StringToCount()) = True Then
  24.     For InnerLoopCounter = LBound(StringToCount) To UBound(StringToCount)
  25.       If MyArray(OuterLoopCounter) = StringToCount(InnerLoopCounter) Then
  26.         CountOfString(InnerLoopCounter) = CountOfString(InnerLoopCounter) + 1
  27.         Exit For
  28.       End If
  29.     Next InnerLoopCounter
  30.   Else
  31.     ReDim Preserve StringToCount(UniqueStringCounter) As String
  32.     ReDim Preserve CountOfString(UniqueStringCounter) As Integer
  33.     StringToCount(UniqueStringCounter) = MyArray(OuterLoopCounter)
  34.     CountOfString(OuterLoopCounter) = 1
  35.     UniqueStringCounter = UniqueStringCounter + 1
  36.   End If
  37.  
  38. Next OuterLoopCounter
  39.  
  40. For InnerLoopCounter = LBound(StringToCount) To UBound(StringToCount)
  41.   MessageString = MessageString & StringToCount(InnerLoopCounter) & " = " & CountOfString(InnerLoopCounter) & vbNewLine
  42. Next InnerLoopCounter
  43.  
  44. MsgBox MessageString
  45.  
  46. End Sub
  47.  
  48. Private Function IsStringInArray(StringToTest As String, ArrayToTestAgainst() As String) As Boolean
  49.  
  50. Dim ForLoopCounter As Integer
  51. Dim UpperBoundsOfArray As Integer, LowerBoundsOfArray As Integer
  52.  
  53. 'if you really want to know more about this test please read this thread
  54. 'http://www.tek-tips.com/viewthread.cfm?qid=1495917
  55. If (Not ArrayToTestAgainst) = -1 Then Exit Function
  56.  
  57. LowerBoundsOfArray = LBound(ArrayToTestAgainst)
  58. UpperBoundsOfArray = UBound(ArrayToTestAgainst)
  59.  
  60. For ForLoopCounter = LowerBoundsOfArray To UpperBoundsOfArray
  61.   If ArrayToTestAgainst(ForLoopCounter) = StringToTest Then
  62.     IsStringInArray = True
  63.     Exit For
  64.   End If
  65. Next ForLoopCounter
  66.  
  67. End Function
  68.  


Good Luck
Dec 5 '09 #2
vb5prgrmr
305 Recognized Expert Contributor
Okay d123, recieved your pm and I did not split an array, I split a string into an array...



Good Luck
Dec 10 '09 #3

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

Similar topics

8
7799
by: Michelle | last post by:
hi, i have created an array from recordset containing user names eg. (davidp, davidp, evenf, patricka, rebeccah) which i have sorted in alphabetical order, but i need to identify duplicates...
13
4211
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number...
16
12052
by: Leon | last post by:
I need a program that generate 5 non-duplicates random number between 1-10 as string values store in an array. Do anybody know of any good books or websites that explain how to generator random...
9
5078
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
16588
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...
8
783
by: sqlservernewbie | last post by:
Hi Everyone, Here is a theoretical, and definition question for you. In databases, we have: Relation
10
4110
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...
10
28069
chandru8
by: chandru8 | last post by:
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
2
2565
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...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7380
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...
1
7039
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...
0
7494
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...
0
5626
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,...
1
5050
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...
0
3192
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...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.