# Frequency of a value in array

**URL:** <https://forum.kirupa.com/t/frequency-of-a-value-in-array/262674>\
**Category:** flash\
**Created:** [June 8, 2008, 10:31pm UTC](https://forum.kirupa.com/t/frequency-of-a-value-in-array/262674 "2008-06-08T22:31:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sizzla](https://avatars.discourse-cdn.com/v4/letter/s/c37758/32.png) [@sizzla](https://forum.kirupa.com/u/sizzla)\
**Post date:** [June 8, 2008, 10:31pm UTC](https://forum.kirupa.com/t/frequency-of-a-value-in-array/262674/1 "2008-06-08T22:31:40Z")

</div>

Hi,

I have an array with duplicate values (strings) in it.  
I would like to have a function that i can call with the string-parameter and returns how many times this string is in the array.

thank you!

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [June 8, 2008, 11:32pm UTC](https://forum.kirupa.com/t/frequency-of-a-value-in-array/262674/2 "2008-06-08T23:32:41Z")

</div>

```auto
function arrayCount(arrayName:Array, arrayElement:String) {
	var n:Number = 0;
	var i:Number = arrayName.length;
	while (i--) {
		if (arrayName* == arrayElement) {
			n++;
		}
	}
	return n;
}
myArray = ["dog", "cat", "mouse", "dog", "cat", "cat", "dog", "mouse", "cat"];

trace(arrayCount(myArray, "dog"));
trace(arrayCount(myArray, "cat"));
trace(arrayCount(myArray, "mouse"));

```

---

<div class="post-metadata">

**Author:** ![sizzla](https://avatars.discourse-cdn.com/v4/letter/s/c37758/32.png) [@sizzla](https://forum.kirupa.com/u/sizzla)\
**Post date:** [June 9, 2008, 10:09am UTC](https://forum.kirupa.com/t/frequency-of-a-value-in-array/262674/3 "2008-06-09T10:09:47Z")

</div>

Thank you, it worked!

---

<div class="post-metadata">

**Author:** ![nathan99](https://avatars.discourse-cdn.com/v4/letter/n/96bed5/32.png) [@nathan99](https://forum.kirupa.com/u/nathan99)\
**Post date:** [June 10, 2008, 2:52am UTC](https://forum.kirupa.com/t/frequency-of-a-value-in-array/262674/4 "2008-06-10T02:52:19Z")

</div>

I think you could do it faster witout using a loop, jsut take the length of the array with the items removed, from the actual length

---

<div class="post-metadata">

**Author:** ![Fidodo](https://avatars.discourse-cdn.com/v4/letter/f/779978/32.png) [@Fidodo](https://forum.kirupa.com/u/Fidodo)\
**Post date:** [June 10, 2008, 4:18am UTC](https://forum.kirupa.com/t/frequency-of-a-value-in-array/262674/5 "2008-06-10T04:18:13Z")

</div>

Just because I can, here’s another way to do it.

```auto

function equals(element:*, index:int, arr:Array):Boolean{
	return element == this;
}
var myArray:Array = ["dog", "cat", "mouse", "dog", "cat", "cat", "dog", "mouse", "cat"];
trace(myArray.filter(equals, "dog").length);
trace(myArray.filter(equals, "cat").length);
trace(myArray.filter(equals, "mouse").length);

```

The filter method of the Array class runs a given function on each array item that returns a boolean. filter then returns an array of all the items that the function returned true on. The second parameter of the filter method maps to the functions this parameter, so that is how I check the element against what is given.

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [June 10, 2008, 7:01am UTC](https://forum.kirupa.com/t/frequency-of-a-value-in-array/262674/6 "2008-06-10T07:01:03Z")

</div>

> [@nathan99;2339810](#):
>
> I think you could do it faster witout using a loop, jsut take the length of the array with the items removed, from the actual length

I’d like to see that…of course, without looping through the array how would you know which items to remove?

> [@Fidodo;2339841](#):
>
> Just because I can, here’s another way to do it.

Again, I’d like to see this working in ActionScript 1.0 or 2.0…neither of which have an Array.filter() method

---

<div class="post-metadata">

**Author:** ![Fidodo](https://avatars.discourse-cdn.com/v4/letter/f/779978/32.png) [@Fidodo](https://forum.kirupa.com/u/Fidodo)\
**Post date:** [June 10, 2008, 9:35am UTC](https://forum.kirupa.com/t/frequency-of-a-value-in-array/262674/7 "2008-06-10T09:35:55Z")

</div>

Oh, duh, I’m in the wrong forum.

---

<div class="post-metadata">

**Author:** ![nathan99](https://avatars.discourse-cdn.com/v4/letter/n/96bed5/32.png) [@nathan99](https://forum.kirupa.com/u/nathan99)\
**Post date:** [June 10, 2008, 10:45am UTC](https://forum.kirupa.com/t/frequency-of-a-value-in-array/262674/8 "2008-06-10T10:45:41Z")

</div>

look up the split() and join() functions
