VI High 15: How to Find and Replace Duplicates From Our Selected Random Array Elements in LabVIEW
In our last episode we made a simple VI to randomly select items from an array but we found that it was prone to finding duplicates. This time around we find out how to avoid those duplicates.
For more on learning LabVIEW, check out the LabVIEW Fundamentals Course offered by Sixclear: sixclear.com.
Experience Level: Basic
(start transcription)
“When we finished VI High #14, we had pulled out some random names from our Name Array, but we noted that we often ran into duplicates and we said that we would be coming back to item 3 in our implementation plan to find and replace those duplicates. There are, again, a few ways to do this but remember the primary characteristic of our implementation should be: robustness.
Robustness.
Let’s first be aware of some of the characteristics of the duplicates. Sometimes we only get one duplicate, or in poker terminology, a pair. Look at Sarah. Sometimes we get two pair, like 2 Aruns and 2 Veronicas. And sometimes we get three of a kind or more, like greedy Mindy here.
So we need to search through this array of indices to find these duplicates. First let’s clean up this block diagram. Then let’s use a For Loop which we’ll auto-index with this array of indices, and we want to compare this element to every other element in the array, which means we’ll bring in the entire array and disable indexing. We’ll want to remove this element we’re checking from the array. And we’ll use the Delete From Array function to do so. The iteration terminal will supply the appropriate index. So now we’ll put another For Loop inside of this one and index the array without the element we’re checking and compare the element we’re checking to every element in here with an equal function. So is this equal to this. If it’s true, then we want to replace that element in this array that we’re checking. So within a case structure, we’re essentially going to duplicate this little block of code over here except we’re doing it for a single element instead of an array. So I hold down shift and click, click, click and then hold down Ctrl, click and drag them over here. This new element I just created is going to replace the element we’re checking, the index of which is here in this iteration terminal of the outer For Loop. And so we’ll use a Replace Array Subset. I’ll hold Ctrl, click and drag to give us more space, delete this auto wiring which wasn’t helpful, wire this array into here, disabling indexing. That’s the index, and the new value.”
“And note that the array that I use the Replace Array Subset with is the full array, not the one from which I had removed that element in question. I’ll wire the resulting array to the border, disable indexing on both of these For Loops, their output. And in the False case of the case structure, we’ll just wire this array through because we didn’t find a match and we’re not changing anything. Now we want these For Loops to keep running until they’ve run through all elements in the array or they’ve found a match. This is a perfect use case of a Conditional For Loop which is available in LabVIEW 8.5.1. and beyond. So I’ll right click on the border of the For Loop, Conditional Terminal, once again I’ll hold down Ctrl click and drag to create some space, and now I’ll wire this boolean to the Conditional Terminal. And we’ll do the same for the outer For loop. And disable indexing, we just want one value.
So what do we do now? Have we entirely fixed the duplicate problem? Well no, this implementation will fix a single pair, if we’re again invoking our poker terminology. But what about the two pair or 3 of a kind? Remember greedy Mindy. This implementation won’t address those problems so we need to take the entire array, after the replacement, and run it through the same process again. How do we do that? We’ll wrap these nested For Loops in a While Loop. Give some space, and we’ll use a shift register to send this array back to the beginning and run it through the same process again. So the input array will no longer come through this tunnel, but rather the shift register.
And what condition will stop the While Loop? We’ll know we don’t need to run the While Loop anymore when both of these For Loops stop running and they didn’t find a match, meaning that this wire will be false. So we’ll change the Conditional Terminal on the While Loop to Continue if True, wire that value to it, disable indexing, and so the While Loop will stop when the Boolean value is false. Now the output from here will be a set of 6 unique indices. So we’ll just drag this little block of code - Ctrl click and drag - over here, and make another output: Real Winners! Excitedly. And run it.
We see that we’re successful, by checking that, in the case where there’s a duplicate in the Winners list, like Santiago, it is replaced with a unique name in the Real Winners! list. Run it a few more times, we have two Xui Lis, and only one in the Real Winners list. Fantastic.
That’s it, this VI is ready for action, which it will see after 11:59PM CST on June 9 when the sweepstakes ends. If you haven’t entered yet, get to it by visiting the instructions at news.sixclear.com. And once again, good luck!”
(end transcription)