Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

compare value in an array

Posted on 2010-11-24
5
576 Views
Last Modified: 2013-12-25
hi,
   I've stuck on a process which need to compare values in a two dimensional array, to determine how many different unique identities are there in such array. would anybody help me with that?
I am reading data into array through database, and it returns something like this.
counting(1,0) = "Mike"
counting(2,0) = "jack"
.
.
.
counting(8,0) = "Mike" --> theres a duplicate
.
.
.
the size of array varied each time i run the solution. but i managed to have all the names in
counting(i,0). if that helps. any help would be appreciated;)

and eventually i need to create a list of unique Names, so that'll minors the range down to like 10 persons in total.
0
Comment
Question by:miketonny
  • 3
5 Comments
 
LVL 10

Expert Comment

by:hosneylk
ID: 34210746
iterate through each element in the array and copy the element to a new array. before adding it to the new array check if the new array already contains it. if it does don't copy it.
0
 
LVL 10

Expert Comment

by:hosneylk
ID: 34210772

Dim unique() As String
        ReDim unique(0)
        Dim valExists As Boolean
        For Each Val As String In counting
            valExists = False
            For Each uniqueVal As String In unique
                If (uniqueVal = Val) Then
                    valExists = True
                    Exit For
                End If
            Next
            If Not valExists Then
                ReDim Preserve unique(unique.Length + 1)
                unique(UBound(unique)) = Val
            End If
        Next

Open in new window

0
 
LVL 13

Expert Comment

by:Daz_1234
ID: 34212250
Hi,

You can also use the dictionary object - dictionary keys must be unique.

The short script below is a example of using this method.

Hope this helps,
Daz.
Dim Counting(50,1)

Set dicList = CreateObject("Scripting.Dictionary")

Counting(0,0) = "Mike"
Counting(1,0) = "Jon"
Counting(2,0) = "Mike"
Counting(3,0) = "Daz"
Counting(4,0) = "Mary"
Counting(5,0) = "Mike"


For i = 0 To UBound(Counting, 1)
    If Counting(i, 0) <> "" Then
        dicList(Counting(i, 0)) = 1
    End If
Next

MsgBox Join(dicList.Keys, " / "),, "Unique Names = " & dicList.Count

Open in new window

0
 
LVL 2

Author Comment

by:miketonny
ID: 34214204
hi hosneylk,
   it did give me the counts in array, but it included all the irrelevant data, as it's a two dimensional array, i do know the usernames are only stored in (1,0), (2,0), ... (x,0). but what you did stores pretty much every different value into the new array, how do i sort them out?
0
 
LVL 10

Accepted Solution

by:
hosneylk earned 125 total points
ID: 34214996

Dim unique() As String
        Dim strVal As String
        ReDim unique(0)
        Dim valExists As Boolean
        Dim i As Long
        For i = LBound(counting, 1) To UBound(counting, 1)
            valExists = False
            strVal = counting(i, 0)
            For Each uniqueVal As String In unique
                If (uniqueVal = strVal) Then
                    valExists = True
                    Exit For
                End If
            Next
            If Not valExists Then
                ReDim Preserve unique(unique.Length + 1)
                unique(UBound(unique)) = strVal
            End If
        Next

Open in new window

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel 2016 loop through 6 43
Using VBScript. How to obtain the recomended paging file size? 8 70
reboot server with scheduled time and week base 4 35
Modify file details 1 15
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question