 Thread: Count number of times a number appears

1. Count number of times a number appears

If i have a set of numbers stored in some alterables values, how would I loop through the values and count the number of times each unique number appears?
For example:
Alterable value 0 = 0
Alterable value 1 = 1
Alterable value 2 = 1000
Alterable value 3 = 500
Alterable value 4 = 1000

I would want my program to output that there is one 0, one 1, one 500, and two 1000s....

Thanks!  Reply With Quote

2. Which platform do you want to do this on? Could you give a little more detail on what you are trying to achieve here at all? I can see how to do what you need, but it doesn't seem like a very efficient solution. Also, what is the range of potential numbers you will be testing for? Is there an upper limit, lower limit, only a set pool of values (such as 100, 200, 300 etc.)  Reply With Quote

3. OK. Here is the method for 0, 1 and 2s

Create a counter for 0, 1, 2s etc.

-> Store the number in a string object, using the "Number to string" expression in the string object expression menu
* Always
- Set NumberVersionOfText\$ to str\$(Number)

-> Do a fast loop (in the system object)
* Always
+ Set "number of 0s" to 0
+ Set "number of 1s" to 0
+ Set "number of 2s" to 0
+ Start loop "Count" Len(String\$("NumberInText)) (in the system object expression menu)

* On loop "count"
* Mid\$(NumberVersionOfText, LoopIndex("Count"), 1) = "0"
- Add 1 to "Number of 0s"

* On loop "count"
* Mid\$(NumberVersionOfText, LoopIndex("Count"), 1) = "1"
- Add 1 to "Number of 1s"

* On loop "count"
* Mid\$(NumberVersionOfText, LoopIndex("Count"), 1) = "2"
- Add 1 to "Number of 2s"

This should work.  Reply With Quote

4. For an arbitrary number of possibilities, I'd create a map (Named Variable Object or anything else that supports key-value pairs) and loop over the list once by using Get Alterable Value with Index N (I can't remember the exact expression):

- Get the alterable value
- Increment the value's key in the map by 1

The map will hold the number of occurrences of each number. In the given example:

Alterable value 0 = 0
Alterable value 1 = 1
Alterable value 2 = 1000
Alterable value 3 = 500
Alterable value 4 = 1000

Loop: Gets 0. Map now contains:
0=1

Loop: Gets 1. Map now contains:
0=1
1=1

Loop: Gets 1000. Map now contains:
0=1
1=1
1000=1

Loop: Gets 500. Map now contains:
0=1
1=1
1000=1
500=1

Loop: Gets 1000. Map now contains:
0=1
1=1
1000=2
500=1

Now loop over each key in the map, and you have the number of occurrences of that number.  Reply With Quote Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•