locked
count the number of elements in an array RRS feed

  • Question

  • Hi all, I apologize for my English, I'm using google translator.
    I would like to create a function that matters to me the number of identical elements within an array.
    In a nutshell if I have an array that contains the elements:  HELLO | GOAL | HELLO | TOAST | HELLO
    The result of the function must be: HELLO -> 3 GOAL -> 1, TOAST -> 1

    Thank you all for the help
    Tuesday, December 6, 2016 11:20 AM

Answers

  • How about this:

    arr="1=HELLO;2=GOAL;3=HELLO;4=TOAST;5=HELLO"
    For i=1 To Array.GetItemCount(arr)
      count[arr[i]]=count[arr[i]]+1
    endfor
    cnt=Array.GetAllIndices(count)
    For i=1 To Array.GetItemCount(count)
      TextWindow.WriteLine(cnt[i]+"  "+count[cnt[i]])
    EndFor
      


    Jan [ WhTurner ] The Netherlands

    • Marked as answer by Carmenello Tuesday, December 6, 2016 4:31 PM
    Tuesday, December 6, 2016 11:45 AM
    Answerer

All replies

  • How about this:

    arr="1=HELLO;2=GOAL;3=HELLO;4=TOAST;5=HELLO"
    For i=1 To Array.GetItemCount(arr)
      count[arr[i]]=count[arr[i]]+1
    endfor
    cnt=Array.GetAllIndices(count)
    For i=1 To Array.GetItemCount(count)
      TextWindow.WriteLine(cnt[i]+"  "+count[cnt[i]])
    EndFor
      


    Jan [ WhTurner ] The Netherlands

    • Marked as answer by Carmenello Tuesday, December 6, 2016 4:31 PM
    Tuesday, December 6, 2016 11:45 AM
    Answerer
  • thank you so much. With 3 simple steps you did what I was doing in 30 lines of code XD
    Tuesday, December 6, 2016 4:31 PM