LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I remove repeated elements in an array

Hi I have an array 

 

1,2,2,2,3,3,4,1,2,3,2

 

How can I remove the repeated elements to have just 1,2,3,4 . The order is not important. Could you please let me know how can I do it in labVIEW.

 

Thanks,

0 Kudos
Message 1 of 38
(10,405 Views)

Step 1: Sort the 1D array

Step 2: wire into a for loop

Step 3: Place first value from array into output array.

Step 4: [inside for loop] Compare current value with previous value (shift register or feedback node)

Step 5: [inside for loop] If value does not equal previous (new value) add to the output array.  (keep array between iterations using shift register)

0 Kudos
Message 2 of 38
(10,392 Views)

Use OpenG, on the Array palette there is a Remove Duplicates From 1D Array.

Message 3 of 38
(10,386 Views)
0 Kudos
Message 4 of 38
(10,361 Views)

This one is much faster (for large arrays)

 

attached vi (LV2011)

 

SR2.png

 

0 Kudos
Message 5 of 38
(10,349 Views)

Example_VI.png

Message 6 of 38
(10,336 Views)

@ouadji wrote:

This one is much faster (for large arrays) 


I suspect that it could be made significantly faster for large arrays, because your search and delete is basically O(N²) (guessing), while the sort method is roughly O(NlogN). Delete from array is also very inefficient to use, because for every single delete operation, all higher elements need to be moved down (Arrays need to be contiguous in memory!). The last element in your array will get moved many times, depending on the number of duplicates. Way too much work!

 

Try to improve it! 😄

Message 7 of 38
(10,331 Views)

@apok :

 

Why complicate things when they can be simple?  Smiley Happy   kudo.

 

@altenbach:

 

all higher elements need to be moved dow, the last element in your array will get moved many times, depending on the number of duplicates

 

thank you altenbach for your explanation

you're absolutely right, I had not looked at it from this angle. Understood!

Indeed, my code is very slow,
the code from Apok is much better and much faster.

0 Kudos
Message 8 of 38
(10,324 Views)

ouadji wrote:

the code from Apok is much better and much faster.


Mine is still orders of magnitude faster than both for large input arrays. 😄

 

For example for a size 50k input i get the following:

 

case 1: all elements are the same:

altenbach: 3ms

ouadji: 1300ms

apok: 25ms

 

case 2: 95% of elements are unique:

altenbach: 7ms

ouadji: 770ms

apok: 3800ms

 

0 Kudos
Message 9 of 38
(10,316 Views)

@altenbach wrote:

ouadji wrote:

the code from Apok is much better and much faster.


Mine is still orders of magnitude faster than both for large input arrays. 😄 ??

 

For example for a size 50k input i get the following:

 

case 1: all elements are the same:

altenbach: 3ms ??

ouadji: 1300ms

apok: 25ms

 

case 2: 95% of elements are unique:

altenbach: 7ms ??

ouadji: 770ms

apok: 3800ms

 


i want to be schooled....

0 Kudos
Message 10 of 38
(10,298 Views)