LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Check array if there is a duplicate value

Solved!
Go to solution

Hello,

 

I know that this should be simple, but I am not converging to a simple solution.

 

I would like to just generate a TRUE value if there is a duplicate value in an array.

 

This is what I have thus far (but doesn't work):

hiNI_0-1719513600878.png

Can you offer some guidance as to a simple elegant solution to return TRUE if there is a duplicate?

 

Thanks for your help and time!

 

0 Kudos
Message 1 of 14
(2,852 Views)

The following code should work. This is going to run O(n^2) so It'll get really slow for larger arrays, but I'm not sure you can do much better with unordered arrays.

dupe.PNG

Message 2 of 14
(2,834 Views)
Solution
Accepted by hiNI

maxnoder1995_0-1719515887326.png

 

Message 3 of 14
(2,812 Views)

Didn't even know there were sets in LabVIEW, so thanks! This is a better solutions than mine for sure.

0 Kudos
Message 4 of 14
(2,807 Views)

@davy.k wrote:

Didn't even know there were sets in LabVIEW, so thanks! This is a better solutions than mine for sure.


It was added in 2020 or something. 

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 5 of 14
(2,750 Views)

@hiNI wrote:

This is what I have thus far (but doesn't work):

hiNI_0-1719513600878.png


This code makes absolutely no sense!

 

  • It does not return anything! (There is no indicator)
  • You search the entire array for an element that is guaranteed to exist, so the search output can never be <0.

 

Yes, Maps and Sets were introduce in LabVIEW 2019, have a look at my presentation for details. They offer a good solution.

 

 

Even without the use of a set, this will probably OK for typical scenarios:

 

altenbach_1-1719686309440.png

 

 

Message 6 of 14
(2,708 Views)

dupe.PNG

Without sets this is possibly a better solution with a runtime of O(n*log(n)), though it might lead to the array being copied.

Message 7 of 14
(2,654 Views)

@davy.k wrote:

dupe.PNG

Without sets this is possibly a better solution with a runtime of O(n*log(n)), though it might lead to the array being copied.


Yes, sorting first can be an advantage for very large arrays and very few duplicates, but why would you need two instances of index array and that +1??? It is sufficient to resize to two outputs and wire [i] to the top index and the second one will automatically be [i]+1.

 

You can also delete the first element, autoindex over the rest, and compare current to previous. Here are two possibilities: Version B is your example, but cleaned up a bit. 😄

 

altenbach_0-1719845715454.png

 

 

 

 

Message 8 of 14
(2,632 Views)

Be aware that anything that relies on an equal comparison will not be able to tell if there are duplicate NaNs in the array.

 

If that is a concern, the "set" or my earlier version will still work correctly!

 

altenbach_1-1719847644008.png

 

 

 

 

Message 9 of 14
(2,615 Views)

Could be a relevant edge case, though checking floating point numbers for equality is code smell and usually a bad idea IMO. I assumed that we're just using floating point in these examples because it's the default.

0 Kudos
Message 10 of 14
(2,599 Views)