删除数组中的负值重复值

时间:2017-08-13 12:27:13

标签: javascript arrays

说我有一个数组:

var arr = [-1, -5, 4, 5, 3];

如何删除数组中任何数字的负数版本?所以输出将是:

[-1, 4, 5, 3]

6 个答案:

答案 0 :(得分:2)

这将过滤掉所有负数且在数组中具有正数的变量

var arr = [-1, -5, 4, 5, 3, -5];

arr = arr.filter(function(a, b){
  if(a < 0 && arr.indexOf(-1*a) > -1){
    return 0;
  }
  if(a < 0 && arr.indexOf(a) != b){
  	return 0;
  }
  return 1;
})

console.log(arr);

答案 1 :(得分:1)

您可以将filter()Math.abs()

一起使用

var arr1 = [-1, -5, 5, 4, 3];
var arr2 = [-1, -5, -5, 4, 3];


function customFilter(arr) {
  return arr.filter(function(e) {
    if (e > 0) return true;
    else {
      var abs = Math.abs(e)
      if (arr.indexOf(abs) != -1) return false;
      else return !this[e] ? this[e] = 1 : false;
    }
  }, {})
}

console.log(customFilter(arr1))
console.log(customFilter(arr2))

答案 2 :(得分:0)

你需要迭代数组,并在第二个数组中添加所有那些没有absolue值的数据:

 #define MEMSIZE (2.6L * 1024L * 1024L * 1024L)
 #include<stdio.h>
 __global__ void add(float *a, float *b, float *c, unsigned long long num)     {
     unsigned long long idx = (blockIdx.x * blockDim.x) + threadIdx.x;
     if(idx < num) {
         c[idx] = a[idx] + b[idx];
     }
 }

 int main() {

     cudaEvent_t start, stop;
     cudaError_t err;

     float *a, *b, *d_a, *c, *d_b, *d_c;
     unsigned long long num = MEMSIZE/4;
     float elapsedTime;

     err = cudaMalloc((void **)&d_a, MEMSIZE);
     if (err != cudaSuccess) {
         printf("failed to allocate memory to d_a\n");
         exit(0);
     }

     err = cudaMalloc((void **)&d_b, MEMSIZE);
     if (err != cudaSuccess) {
         printf("failed to allocate memory to d_b\n");
         exit(0);
     }

     err = cudaMalloc((void **)&d_c, MEMSIZE);
     if (err != cudaSuccess) {
         printf("failed to allocate memory to d_c\n");
         exit(0);
     }

a = (float *)malloc(MEMSIZE);
if(a==NULL) {
    printf("Failed to allocate memory to a");
    exit(0);
}

b = (float *)malloc(MEMSIZE);
if(b==NULL) {
    printf("Failed to allocate memory to b");
    exit(0);
}
c = (float *)malloc(MEMSIZE);
if(c==NULL) {
    printf("Failed to allocate memory to c");
    exit(0);
}

for(unsigned long long i=0; i<num; i++) {
    float v = i/1000.0;
    a[i] = v;
    b[i] = v;
}

err = cudaMemcpy(d_a, a, MEMSIZE, cudaMemcpyHostToDevice);
if (err != cudaSuccess) {
    printf("failed to copy memory from host to device\n");
    exit(0);
}

err = cudaMemcpy(d_b, b, MEMSIZE, cudaMemcpyHostToDevice);
if (err != cudaSuccess) {
    printf("failed to copy memory from host to device\n");
    exit(0);
}

int thr = 1024;
long int bloc = (num/thr)+1;
printf("Blocks per grid: %ld", bloc);
printf("\nThreads per bloc: %d", thr);

cudaEventCreate(&start);
cudaEventRecord(start, 0);

add<<<bloc, thr>>>(d_a, d_b, d_c, num);

cudaError_t errSync  = cudaGetLastError();
cudaError_t errAsync = cudaDeviceSynchronize();
if (errSync != cudaSuccess) {
    printf("Sync kernel error: %s\n", cudaGetErrorString(errSync));
    exit(0);
}
if (errAsync != cudaSuccess) {
    printf("Async kernel error: %s\n", cudaGetErrorString(errAsync));
    exit(0);
}

cudaEventCreate(&stop);
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&elapsedTime, start, stop);;

printf("\nGPu time --> %f milliseconds\n", elapsedTime);
printf("Gpus time --> %f seconds\n", elapsedTime/1000);

err = cudaMemcpy(c, d_c, MEMSIZE, cudaMemcpyDeviceToHost);
if (err != cudaSuccess) {
    printf("failed to copy memory from Device to host\n");
    exit(0);
}
free(a); free(b); free(c);

cudaFree(d_a);
cudaFree(d_b);
cudaFree(d_c);

return 0;
}

这是改编自this answer,非常相似。

答案 3 :(得分:0)

您可以检查标志或是否包含绝对值。

var array = [-1, -5, 4, 5, 3],
    result = array.filter((a, _, aa) => a >= 0 || !aa.includes(Math.abs(a)));
    
console.log(result);

答案 4 :(得分:0)

使用过滤器

var arr = [-1, -5, 4, 5, 3, 4, -6, 4, 6];

console.log(removeNegativesCommon(arr));

function removeNegativesCommon(arr){
  return arr.filter((el) => {
    if( el < 0 && arr.indexOf(Math.abs(el)) != -1){
      return false;
    } else {
      return el;
    }
  })
 }

答案 5 :(得分:0)

这是一个不使用重复indexOf的版本。它使用来自my previously linked post(uniq函数)的解决方案以及先前删除已包含为正数的否定值。

&#13;
&#13;
var arr = [-1, -5, 4, 5, 3];

function uniq(a) {
    var seen = {};
    return a.filter(function(item) {
        return seen.hasOwnProperty(item) ? false : (seen[item] = true);
    });
}

function preRemoveNegatives(a) {
  let table = {};
  a.filter(e => e >= 0).forEach(e => table[e] = true);
  return a.filter(e => e >= 0 || !table[-e]);
}

console.log(uniq(preRemoveNegatives(arr)));
&#13;
&#13;
&#13;