10-12-2011 12:44 PM
If a 1-D array has duplicate elements, how do you get rid of the duplicate elements?
Here is my input array: A A A A A B B B C C C D D E E E E E G
Here is the desired output array: A B C D E G
Note that the output array does not contain empty elements. It only has 6 elements.
Solved! Go to Solution.
10-12-2011 01:05 PM
Basically, you just step through the array one element at a time and do "If element equals previous, ignore it... if element does not equal previous, keep it."
10-12-2011 01:12 PM
Thank you!
I tried comparing elements pair-wise, similar to your logic, but my output array had empty elements in it. It size was not 6.
Yours does exactly what I want. Thanks!!
10-12-2011 01:16 PM
A few things you might want to keep in mind:
The upper shift register tracks the previous element. You want to initialize it with a value that you are sure is not going to be in the array. If you initialize it with the same thing as the first element in the array, it's going to miss collecting that first value, because it thinks it's already got it.
Depending on your application, you might want to sort the array first. If you don't sort the array, AAABBBCCCAAABBBCCC will come out as ABCABC because the second run of each letter will get caught . If you do sort it, it will come out as ABC.
Another way of doing it uses the "Search 1D array" function wich returns a -1 if the element is not found and a positive number (or zero) if the element is found. In this case, you don't need to store the previous element because it will search through the whole array each time. Also, you don't need to sort the array. It will always reduce the array to unique elements, even if they are out of order.
10-12-2011 01:24 PM
Conceptually, the trick here is to start with an empty array and build it up by selectively adding elements as you go.
Some people want to start with the fully populated array and then selectively delete elements from it. You can make that work, if you really want to...but I don't like it as much.
Usually, I like to avoid shift registers because they look sloppy (adding wires clear across the loop), but I think they're the best solution here. As a general rule of thumb, if I know the output array will be the same size as the input array, I use an auto-indexing tunnel. If the size of the output array is unknown at first, I use a shift register and build it up as I go.
10-12-2011 01:38 PM
Thank you!! I really appreciate not only your solution but also the extra piece of knowledge that you shared with me about
the use of shift registers, and the concept behind your VI.
Thanks a bunch.
10-12-2011 01:47 PM
Here is an example that agressively removes all duplicated elements even if they are not consecutive.