从数组中删除三元组项

时间:2016-05-13 05:57:27

标签: javascript arrays methods

我确实想从我的数组中删除出现(03)次的数字。我不想删除重复项,因为它仍然会留下我不想要的数字。重复的数字是143& 187。

var number = [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 143, 143, 143, 149, 151, 157, 163, 167, 173, 179, 181, 187, 187, 187, 191];
for (i = 0; i < number.length; i++) {
    if ((number[i] == number[i + 1]) && (number[i + 1] == number[i + 2])) {
        document.write(number[i]);
        document.write(number[i + 1]);
        document.write(number[i + 2] + "<br>");
        number.splice(number[i - 1], number[i], number[i + 1], number[i + 2]);
    }
};
document.write(number);

4 个答案:

答案 0 :(得分:0)

  

我想删除重复2到3次的所有数字。

Array.prototype.splice()的参数是

  1. 起始索引
  2. 从该索引开始删除的元素数
  3. 要添加到阵列的元素(可选)
  4. 您的代码应为:

    for (i = 0; i < number.length; i++) {
        if (number[i] == number[i + 1])
            if (number[i] == number[i + 2])
                number.splice(i, 3);
            else
                number.splice(i, 2);
    }
    

    如果您想要一种更加动态的方法来删除重复(甚至超过3),请使用内循环:

    for (i = 0; i < number.length; i++) {
        var inARow = 1;
        while (number[i] == number[i + inARow])
            inARow++;
        if (inARow > 1)
            number.splice(i, inARow);
    }
    

    Working JSFiddle

答案 1 :(得分:0)

var number = [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 143, 143, 143, 149, 151, 157, 163, 167, 173, 179, 181, 187, 187, 187, 191],
    isDuplicate = false,
    totalDuplicate = 0,
    thresHoldDuplicating = 3,
    tmpNumber = number[0],
    resultNumber = [tmpNumber];

var resetFn = function () {
    isDuplicate = false;
    totalDuplicate = 0;
}
for (i = 1; i < number.length; i++) {
    if (tmpNumber !== number[i]) {
        tmpNumber = number[i];
        resetFn();
        resultNumber.push(number[i]);
        continue;
    }

    isDuplicate = true;
    totalDuplicate += 1;
    
    if (totalDuplicate === thresHoldDuplicating - 1) {
        resetFn();
        resultNumber.splice((resultNumber.length - 1), thresHoldDuplicating);
    }
};
document.write('before: ' + number + '\n');
document.write('after: ' + resultNumber);

这个想法是确定序列中有多少重复的数字。如果它重复3次,则从数组中删除重复的数字。

答案 2 :(得分:0)

试试这个

var number = [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 143, 143, 143, 149, 151, 157, 163, 167, 173, 179, 181, 187, 187, 187, 191,319,319,319,323,323,323];
for (i = 0; i < number.length; i++) {
  var cnt = 1,t = i,k = 0;
  while (number.indexOf(number[i], t==number.length-1?t:t + 1) >= 0 && k < number.length) {
    cnt++;
    var t = number.indexOf(number[i], t + 1);
    k++;
  }
  if (cnt >= 3) {
    var f = number[i];
    console.log("deleted " + f)
    while (number.indexOf(f) >= 0) {
      number.splice(number.indexOf(f), 1);
    }
  }
};
document.write(number);

答案 3 :(得分:-1)

我尝试了以下内容。您可以使用以下方法删除3个或更多连续出现次数。

var number = [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 143, 143, 143, 143, 149, 151, 157, 163, 167, 173, 179, 181, 187, 187, 187, 191];
var prev;
var occurance = 1;
var deletePos = [];
var offset = 0;
/* Get 3+ repeat positions. */
for(i = 0; i < number.length; i++){
  if(occurance >= 3 && prev != number[i])
    deletePos.push({occurance: occurance, index:i});
  occurance = (prev == number[i]) ? (occurance + 1) : 1;
  prev = number[i];
}
/* Delete items in each positions. */
for(j = 0; j < deletePos.length; j++){
  var reObj = deletePos[j];
  number.splice((reObj.index - offset - reObj.occurance), reObj.occurance);
  offset += reObj.occurance;
}
document.write(number);