06-27-2024 01:41 PM
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):
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!
Solved! Go to Solution.
06-27-2024 02:03 PM - edited 06-27-2024 02:08 PM
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.
06-27-2024 02:18 PM
06-27-2024 02:29 PM
Didn't even know there were sets in LabVIEW, so thanks! This is a better solutions than mine for sure.
06-28-2024 03:22 AM
@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.
06-29-2024 01:41 PM
@hiNI wrote:
This is what I have thus far (but doesn't work):
This code makes absolutely no sense!
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:
07-01-2024 08:34 AM
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.
07-01-2024 10:00 AM
@davy.k wrote:
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. 😄
07-01-2024 10:27 AM - edited 07-01-2024 10:50 AM
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!
07-01-2024 11:21 AM
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.