求教如何求取数组内相同值得个数,谢谢!!!
如:a(10)
a(0)=3
a(1)=1
a(2)=2
a(3)=2
a(4)=6
a(5)=3
a(6)=2
a(7)=2
a(8)=1
a(9)=1我想得到值为1的个数为3
我想得到值为2的个数为4
我想得到值为3的个数为2
我想得到值为6的个数为1
注:数组内最大值已知

解决方案 »

  1.   

    dim i as integer,j as integer,k as integer
    dim bFound as boolean
    dim b()
    dim c()
    redim b(lbound(a) to ubound(a))
    redim c(lbound(a) to ubound(a))
    k=-1for i=lbound(a) to ubound(a)
       bFound=False
       for j=lbound(b) to k
          if b(j)=a(i) then
             bFound=True
             c(j)=c(j)+1
             Exit For
          end if
       next
       if not bFound then       
          k=k+1
          b(k)=a
          c(k)=1
       end if
    nextfor i=lbound(b) to ubound(b)
       debug.print "有" & b(i) & "個" & c(i)
    next
      

  2.   

    for i=lbound(b) to ubound(b)
       debug.print "有" & b(i) & "個" & c(i)
    next
    --------
    更正為
    for i=lbound(b) to k
       debug.print "有" & b(i) & "個" & c(i)
    nextErase b
    Erase c
      

  3.   

    也可以join成一个字符串用replace来计算。
      

  4.   

    谢谢各位,有一句出错:(If b(j) = A(i) Then)
    Private Sub Form_Load()
    Dim i As Integer, j As Integer, k As Integer
    Dim bFound As Boolean
    Dim A(10) As Integer
    Dim b()
    Dim c()
    A(0) = 3
    A(1) = 1
    A(2) = 2
    A(3) = 2
    A(4) = 6
    A(5) = 3
    A(6) = 2
    A(7) = 2
    A(8) = 1
    A(9) = 1ReDim b(LBound(A) To UBound(A))
    ReDim c(LBound(A) To UBound(A))
    k = -1For i = LBound(A) To UBound(A)
       bFound = False
       For j = LBound(b) To k
          If b(j) = A(i) Then
             bFound = True
             c(j) = c(j) + 1
             Exit For
          End If
       Next
       If Not bFound Then
          k = k + 1
          b(k) = A
          c(k) = 1
       End If
    NextFor i = LBound(b) To k
       Debug.Print "有" & b(i) & "個" & c(i)
    NextErase b
    Erase c
    End Sub
      

  5.   

    如果愿意连接数据库可能会更简单
    Private Sub Command1_Click()
    Dim cn As New ADODB.Connection
    Dim rs As ADODB.Recordset
      cn.ConnectionString = "Provider=SQLOLEDB;Server=127.0.0.1;uid=sa;pwd=;"
      cn.Open
      Set rs = cn.Execute("select distinct f,count(*) from (select 1 as f union all select 2 union all select 1) t1 group by f")
      Do While Not rs.EOF
        Debug.Print rs.Fields(0) & "  " & rs.Fields(1)
        rs.MoveNext
      Loop
      cn.Close
    End Sub
    就是连接数据库的时间比起普通计算都来得长
      

  6.   

    值为X 的个数为: UBound(Filter(a, x)) + 1
      

  7.   

    利用 ListBox 统计:
    在窗体上添加一个 List1(可以将 Sorted 属性设置为 True)"user32" Alias "SendMessageA" (ByVal hWND As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, _
    ByVal lParam As String) As LongPrivate Const LB_FINDSTRINGEXACT = &H1A2    '在 ListBox 中精确查找
    Private Const CB_FINDSTRINGEXACT = &H158    '在 ComboBox 中精确查找Private Sub Form_Click()
    Dim a(9) As Integer
    Dim i As Integer, n As Integera(0) = 3
    a(1) = 1
    a(2) = 2
    a(3) = 2
    a(4) = 6
    a(5) = 3
    a(6) = 2
    a(7) = 2
    a(8) = 1
    a(9) = 1For i = 0 To 9
        n = SendMessagebyString(List1.hWND, LB_FINDSTRINGEXACT, -1, CStr(a(i)))
        If n < 0 Then
            List1.AddItem CStr(a(i))
        Else
            List1.ItemData(n) = List1.ItemData(n) + 1
        End If
    Next iFor i = 0 To List1.ListCount - 1
        Debug.Print List1.List(i), List1.ItemData(i) + 1
    Next i
    End Sub运行结果
    1              3 
    2              4 
    3              2 
    6              1
      

  8.   

    Dim a(9) As Integer
    Dim x() as integera(0) = 3
    a(1) = 1
    a(2) = 2
    a(3) = 2
    a(4) = 6
    a(5) = 3
    a(6) = 2
    a(7) = 2
    a(8) = 1
    a(9) = 1x=Filter(a,"1",False)
    msgbox "1有多少個?" & vbcrlf & ubound(a)-ubound(x)
      

  9.   

    另类一点~
    Dim a(), str, b, l
    a = Array(3, 1, 2, 2, 6, 3, 2, 2, 1, 1)
    str = Join(a) & " "
    Do While str <> ""
        b = Left(str, InStr(str, " ")): l = Len(str)
        str = Replace(str, b, "")
        MsgBox b & "有几个?" & vbNewLine & (l - Len(str)) / Len(b)
    Loop