Counting items in a array

  • Zarwadi
  • Born
  • Born
  • Zarwadi
  • Posts: 1
  • Loc: UK

Post 3+ Months Ago

Hi I'm new here can someone help me i need to know how to count how many times a item appears in a array. this is driving me mad. Here my array

(female/femaleimages/April07003.jpg,female/femaleimages/April07003.jpg,female/femaleimages/April07003.jpg,female/femaleimages/April07004.jpg,female/femaleimages/April07004.jpg) :(
kind regards
zar
  • IceCold
  • Guru
  • Guru
  • User avatar
  • Posts: 1254
  • Loc: Ro

Post 3+ Months Ago

One simple solution would be to first sort the items.
Next thing is:
Code: [ Select ]
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;
  1. nIndex=0;
  2. nItemsCount = arrayItems.Length;
  3. crtItem = "";
  4. arrayItemAppear = new Array();
  5. nItemAppearCount = 0;
  6. nAppearIndex = 0;
  7. while (nIndex < nItemsCount)
  8. {
  9.   if (crtItem != arrayItems[nIndex])
  10.   {
  11.     arrayItemAppearCount[nAppearIndex] = nItemAppearCount;
  12.     arrayItemAppear[nAppearIndex] = crtItem;
  13.     crtItem = arrayItems[nIndex];
  14.     nAppearIndex++;
  15.     nItemAppearCount = 0;
  16.   }
  17.   else
  18.    nItemAppearCount++;
  19.  nIndex++;
  20. }
  21.  // set last item
  22.  arrayItemAppearCount[nAppearIndex] = nItemAppearCount;
  23.  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.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 8 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.