counting occurance of an value in a list

notedop

Member
Licensed User
Longtime User
I'd want to know how many times a value is mentioned in a list. For instance list with following values:
blue
blue
green
yellow

needs to output

blue - 2
green - 1
yellow - 1

This is what Ive come up with, but it wont work :S

B4X:
Sub get_percentages(l As List) As List

Dim templist As List
Dim S As String
Dim output As List

l.Sort(True)

output.Initialize
templist.Initialize
templist = l

For i=0 To 3
   S = l.Get(i)
      For i=0 To templist.Size -1
         If S = templist.Get(i) Then
         Dim P As percentage
         P.name = S
         P.number = P.number +1
         templist.Set(i, "counted")
         End If
      Next
   If P.number > 0 Then output.Add(P)
Next

For i=0 To output.Size-1
   Dim P As percentage
   P= output.Get(i)
   P.percentage = P.number/l.Size
   output.Set(i, P)
Next

Return output

End Sub
 

notedop

Member
Licensed User
Longtime User
solution:

B4X:
Type percentage(name As String, percentage As Double, number As Int)

B4X:
Sub get_percentages(l As List) As List

Dim check As Boolean
Dim returnlist As List
Dim total As Int


total = l.Size
Log(total)

returnlist.Initialize
check = False


For i=0 To l.Size-1
   Dim p As percentage
   check = False
   p.name = l.Get(i)
   For j=0 To i-1
      If l.Get(i) = l.Get(j) Then check = True      
   Next
   
   If check = False Then
   p.number = find_occurance(l,l.Get(i))
   p.percentage = (p.number / total)
   returnlist.Add(p)
   End If
Next

Return returnlist


End Sub

Sub find_occurance(l As List, s As String) As Int

Dim counter As Int

For i=0 To l.Size-1
   If l.Get(i)=s Then
      counter = counter+1
   End If
Next

Return counter

End Sub
 
Last edited:
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
B4X:
Dim lst,lst2 As List
Dim k,k0,N As Int 
lst.Initialize :lst2.Initialize 
N=99
For k=0 To N
    lst.Add ("item" & Rnd(1,11))
Next
lst.Sort (True)
k0=0
For k=0 To N-1
    If lst.Get(k)<>lst.Get(k+1) Then
        lst2.Add (lst.Get(k) & " - " & (k+1-k0))
        k0=k+1
    End If
Next
lst2.Add (lst.Get(N) & " - " & (N+1-k0))
    
    
Msgbox(lst,"input")
Msgbox(lst2,"result")
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
This exercise is ideal for a list generated from a SQLite table. Here is the full code:
B4X:
MyList.Initialize
txt="SELECT Color , COUNT(Color) AS ColorCount FROM MyTable " _
& "GROUP BY Color ORDER BY Color"
Cursor1=SQL1.ExecQuery(txt)
Cursor1.Position=0
For i=0 To Cursor1.RowCount-1
   Cursor1.position=i
   MyList.Add(Cursor1.GetString("Color") & Chr(9) & Cursor1.GetString("ColorCount"))
Next
Msgbox(MyList,"Final result")
 
Upvote 0

notedop

Member
Licensed User
Longtime User
Hi Mahares,

thanks, this might be usefull in future projects. I have some experience with SQL because of Access and our WMS called Exceed.
At this point i'm not using a database for android apps.

And MC73, I like your approuch of things :sign0188:
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
you could put the results in a map, using the text as the key and the count as the value.

loop through your list with code like this:

If Map.ContainsKey(l (loopval)) Then
count = Map.Get (l (loopval))+1
else
count = 1
end if
Map.Put(l(loopval), count)
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…