仅删除ArrayList中重复的元素之一

时间:2019-02-15 20:07:55

标签: javascript arraylist filter duplicates google-cloud-firestore

我有一些数组,每次新的数组都充满不同的值。因此,数组有时包含多个相同值。因此它看起来像这样:[0, 0, 0, 2, 3, 0]。我希望能够删除一个目标值实例。例如,如果目标是0,则数组可能看起来像这样:[0, 0, 2, 3, 0]

目前,我正在使用以下代码:
var new_list = grades.filter(e => e !== grade);
注意:成绩是我从数据库中获得的ArrayList。但是此行将删除所有0。但是我只想删除0之一。
编辑:
尝试过这样的事情:

let grades = doc.data()[grade_type] || [];
var elementIndex = grades.indexOf(grade);
grades.splice(elementIndex);

但是不起作用。示例ArrayList [0, 0, 0]。输出[]

〜filip

4 个答案:

答案 0 :(得分:1)

检测到哪个元素重复后,可以使用splice

grades.splice(elementIndex,1);

答案 1 :(得分:0)

您可以删除具有相同值的第二次出现,并保留所有其他值。

var array = [0, 0, 0, 2, 3, 0, 3, 3],
    f = {};

array = array.filter(v => f.all || (f.all = !(f[v] = (f[v] || 0) + 1 !== 2)));

console.log(array);

答案 2 :(得分:0)

更新: 以下内容将删除重复项的第二个实例(我们最早可以检测到实际上存在重复项):

function removeOneTargetDuplicate(list, target) {
  let result = [];
  let count = 0;

  for (let i = 0; i < list.length; i++) {
    if (list[i] === target) {
      count++;
      if (count === 2) {
        continue;
      }
    }
    result.push(list[i]);
  }
  return result;
}

以下解决方案将删除重复项的第一个实例,尽管复杂度更高,但仍具有线性时间复杂度:

function removeOneTargetDuplicate(list, target) {
  let result = [];
  let duplicate = false;

  for (let i = 0; i < list.length; i++) {

    // duplicate will only be true if we've removed the first duplicate
    if (list[i] === target && !duplicate) {

      // if we've found the target, check the rest of the array for a duplicate
      for (let j = i + 1; j < list.length; j++) {
        if (list[j] === target) {
          duplicate = true;
        }
      }

      // if that duplicate was found, skip to the next iteration of the for loop and don't add to the result, effectively removing the first instance of the target
      if (duplicate) {
        continue;
      }
    }
    result.push(list[i]);
  }
  return result;
}

答案 3 :(得分:0)

请检查下一种方法,这里我们遍历数组,直到找到要删除的元素的第一个重复项;找到后,我们使用splice()删除它。

const grades = [0, 0, 1, 0, 1, 2, 3, 2, 3, 4];

const rmOneDupFrom = (arr, n) =>
{
    let nCounter = 0;

    for (let i = 0; i < arr.length; i++)
    {
        nCounter = arr[i] === n ? nCounter + 1 : nCounter;

        if (nCounter > 1)
        {
            arr.splice(i, 1);
            return;
        }
    }
}

console.log("Original => ", JSON.stringify(grades));

// Remove one duplicate of element 3.
rmOneDupFrom(grades, 3);
console.log("one 3 removed => ", JSON.stringify(grades));

// Remove one duplicate of element 0.
rmOneDupFrom(grades, 0);
console.log("one 0 removed => ", JSON.stringify(grades));

// Try to remove one duplicate of element 4.
rmOneDupFrom(grades, 4);
console.log("none removed (4 hasn't duplicates) => ", JSON.stringify(grades));

您应该注意,这种方法会变异原始数组,以防万一,您可以在使用slice()将原始数组传递到rmOneDupFrom()之前对其进行复制,例如:

let grades = [0, 0, 1, 0, 1, 2, 3, 2, 3, 4];

const rmOneDupFrom = (arr, n) =>
{
    let nCounter = 0;

    for (let i = 0; i < arr.length; i++)
    {
        nCounter = arr[i] === n ? nCounter + 1 : nCounter;

        if (nCounter > 1)
        {
            arr.splice(i, 1);
            return;
        }
    }
}

let gradesCopy = grades.slice(0);
rmOneDupFrom(gradesCopy, 3);
console.log("Original => ", JSON.stringify(grades));
console.log("Copy => ", JSON.stringify(gradesCopy));