Example Code

Count the Number of Instances of each Element in an Array

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Overview

This simple VI will allow you to total up the number of times each element appears in an array.


Description

By default it works with Double arrays, but by changing the data type of Input Array and Elements Found (constant and indicator) the logic will work with any array. This VI iterates through the Input Array and accumulates two arrays using shift registers.

 

1. An Elements Found array consisting of the unique members of the Input Array in the order that they were found.

2. A Count array that stores the total counts of the corresponding items in Elements Found.

 

First, for each member of the input array, it checks to see if it has been previously found and adds it to the Elements found array if not. When a new item is added to Elements Found, a new member (value of 1) is added to Count. When an item that has already been found is processed, the corresponding count is incremented.


Requirements: LabVIEW 2012 (or compatible). Hardware: none.


Steps to Implement or Execute Code

This VI is probably more useful as a subVI but it can be used stand-alone.

1. Initialize array.
2. Run the VI.


Additional Information or References:

 

Count the Number of Instances of each Element in an Array FP.png

 

Count the Number of Instances of each Element in an Array sn.png


 **This document has been updated to meet the current required format for the NI Code Exchange.**

------
John.P | Certified LabVIEW Architect | NI Alliance Member

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
azadplc007
Member
Member
on

this Vi gives some hints to me.... but I need counting the number of instances in string array ...can anyone help me out with this....

Rikko
NI Employee (retired)
on

That's easy!  Just replace Input Array and Elements Found with string array.

don_Micheal
Member
Member
on

Thank you for sharing this VI.

I have a question: How can we obtain the index of each element?

For example, I have an array: 1,2,3,5,2,1,4,5,3. I want to get the indexes of each element such as: 1(0,5);2(1,4);3(2,8);4(6);5(3,7).

 

Thank you!