LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Delete, Collapse, Array Duplicate Elements

Solved!
Go to solution

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.

0 Kudos
Message 1 of 7
(6,985 Views)
Solution
Accepted by topic author murchak

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."

 

 

Download All
Message 2 of 7
(6,979 Views)

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!!

0 Kudos
Message 3 of 7
(6,974 Views)

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.

 

 

 

Download All
0 Kudos
Message 4 of 7
(6,970 Views)

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.

Message 5 of 7
(6,968 Views)

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.

0 Kudos
Message 6 of 7
(6,964 Views)

remove dups.png

 

Here is an example that agressively removes all duplicated elements even if they are not consecutive.


"Should be" isn't "Is" -Jay
Message 7 of 7
(6,959 Views)