One simple solution would be to first sort the items.
Next thing is:
nIndex=0;
nItemsCount = arrayItems.Length;
crtItem = "";
arrayItemAppear = new Array();
nItemAppearCount = 0;
nAppearIndex = 0;
while (nIndex < nItemsCount)
{
if (crtItem != arrayItems[nIndex])
{
arrayItemAppearCount[nAppearIndex] = nItemAppearCount;
arrayItemAppear[nAppearIndex] = crtItem;
crtItem = arrayItems[nIndex];
nAppearIndex++;
nItemAppearCount = 0;
}
else
nItemAppearCount++;
nIndex++;
}
// set last item
arrayItemAppearCount[nAppearIndex] = nItemAppearCount;
arrayItemAppear[nAppearIndex] = crtItem;
- nIndex=0;
- nItemsCount = arrayItems.Length;
- crtItem = "";
- arrayItemAppear = new Array();
- nItemAppearCount = 0;
- nAppearIndex = 0;
- while (nIndex < nItemsCount)
- {
- if (crtItem != arrayItems[nIndex])
- {
- arrayItemAppearCount[nAppearIndex] = nItemAppearCount;
- arrayItemAppear[nAppearIndex] = crtItem;
- crtItem = arrayItems[nIndex];
- nAppearIndex++;
- nItemAppearCount = 0;
- }
- else
- nItemAppearCount++;
- nIndex++;
- }
- // set last item
- arrayItemAppearCount[nAppearIndex] = nItemAppearCount;
- arrayItemAppear[nAppearIndex] = crtItem;
after this, in arrayItemAppear you have the item name and in arrayItemAppearCount the number of appearances.
to display:
[code]
for (nIndex=0; nIndex<arrayItemAppear.Length; nIndex++)
trace(arrayItemAppear[nIndex] +" appears " + arrayItemAppearCount [nIndex] + " times"
[code]
Another approach would be to get the first item, search for it's occurrence in the rest of the array, store it in an array (arrayItemAppear) and it's number of appearances in another array (arrayItemAppearCount ).
Then go to next item, see if it already exists in the arrayItemAppear. If it does, go to next item. If it doesn't, keep searching for it in the rest of the array. And so on.
This approach will be faster, and to make it even faster, after you find an item, set it to empty -> "".
i.e.:
index = 0;
originalArray: "item1", "item2", "item3", "item1", "item3", "item1", "item2", "item4"
step1:
originalArray[index] = "item1"
is (originalArray[index] == "") ?
if true,
skip to next item;
else
arrayItemAppear[indexAppear] = originalArray[index];
appearCount = 1;
indexAppear++;
search in the rest of the items for the appearance of originalArray[index];
when you find one, set it to "" then appearCount++;
at the end of search, set arrayItemAppearCount = appearCount;
index++;
step2:
originalArray: "", "item2", "item3", "", "item3", "", "item2", "item4"
repeat step1
step3:
originalArray: "", "", "item3", "", "item3", "", "", "item4"
repeat step1
Hope you understood the idea.
“True mastery transcede any particular art. It stems from mastery of oneself - the ability, developed throgh self-discipline, to be calm, fully aware, and complety in tune with oneself and the surroundings. Then, and only then, can a person know himself. ”