Question: I was trying to clean "NaN" variables in a given array example;
My input array A is:

```
float A[] = { 5.0f, NAN, 1.5f, 0.0f, 1.75f };

```

Expected output array A is:

```
{5.0f,1.5f, 0.0f, 1.75f}

```

I can delete each NAN and shift the elements one time to left.
But if the array is too big to handle, it will become inefficient algorithm.

I couldn't find any solution in C.
So my question is there any more efficient solution than these approach?

Regards.

Answer: 
There is an `O(n)` solution using [two pointers technique](https://leetcode.com/articles/two-pointer-technique/?page=1#). It's posible to swap all `NaN` terms to the end of the array while preserving relative ordering of non `NaN` terms.  

`p` keeps the record of how many non `NaN` terms are encountered and its returned to keep track of current size of `A`.

```c
int moveNAN(float A[], int n)
{
    int p = 0;
    for(int i = 0; i < n; ++i)
        if(!isnan(A[i]))  // if `isnan` is not defined use `A[i] == A[i]` (NaN values return false)
        {
            if(p != i)
                swap(A[i], A[p]);
            ++p;
        }
    return p;
}

```
> 
> Eliminating NaN Values from an array in C
> 
> 
> 

There is little special here about NaN. To eliminate a value from an array, walk the array.

```
// `isnan()`
#include <math.h>

// In place solution
float A[] = { 5.0f, NAN, 1.5f, 0.0f, 1.75f };
size_t n = sizeof A/sizeof A[0];
size_t w = 0; 
for (size_t r = 0; r < n; r++) {
  if (!isnan(A[r]) {
    A[w++] = A[r];
  }
}
// Array size is still `n`, yet the first `w` elements are of interest.

```

To make a new array, code could form a VLA and walk `A[]` twice, first to determine size, 2nd to read and copy.

```
// New array solution
float A[] = { 5.0f, NAN, 1.5f, 0.0f, 1.75f };
size_t n = sizeof A/sizeof A[0];

size_t new_n = 0;
for (size_t r = 0; r < n; r++) {
  if (!isnan(A[r]) new_n++;
}

if (new_n == 0) TBD_code();  // VLA must be at least 1
float new_A[new_n];
size_t w = 0; 
for (size_t r = 0; r < n; r++) {
  if (!isnan(A[r]) {
    new_A[w++] = A[r];
  }
}

```