我有这个数组
var a = [5] , count = 5;
我想知道这个数组(a)上缺少的数字,结果必须是
1,2,3,4
我只是尝试这个但是失败了
var missing = [];
for ( var j = 0; j < a.length; j++ ) {
for ( var i = 1; i <= count; i++ ) {
if (a[j] != i) {
missing.push( i );
}
}
}
我马上打电话给它
1,2,3,4
为(a)数组添加一些值,如
a = [2,3,4,5]
它给我这个
[1,3,4,5,1,2,4,5,1,2,3,5,1,2,3,4]
我怎样才能解决它找到计数值缺失的数字
注意*找到计数值缺失的数字
答案 0 :(得分:8)
您可以使用indexOf
功能:
var a = [5], count = 5;
var missing = new Array();
for(var i=1;i<=count;i++) {
if(a.indexOf(i) == -1){
missing.push(i);
}
}
console.log(missing); // to check the result.
答案 1 :(得分:4)
使用 indexOf()
检查数组中的元素
var a = [5],
count = 5,
missing = [];
for (var i = 1; i <= count; i++) {
if (a.indexOf(i) == -1) {
missing.push(i);
}
}
document.write('<pre>' + JSON.stringify(missing) + '</pre>');
答案 2 :(得分:4)
单线ES6即兴演奏:
let missingNumbers = (a, l=true) => Array.from(Array(Math.max(...a)).keys()).map((n, i) => a.indexOf(i) < 0 && (!l || i > Math.min(...a)) ? i : null).filter(f=>f);
默认情况下,返回多个数字序列中缺少的数字:
array = [2, 5, 9]
missingNumbers(array)
// returns [3, 4, 6, 7, 8]
但是你可以将low-value标志设置为false并从1开始得到结果:
missingNumbers(array, false)
// returns [1, 3, 4, 6, 7, 8]
missingNumbers([5])
// returns [1, 2, 3, 4]
或简单地定义没有标志的函数
let missingNumbers = (a) => Array.from(Array(Math.max(...a)).keys()).map((n, i) => a.indexOf(i) < 0? i : null).filter(f=>f);
答案 3 :(得分:3)
处理动态最小和最大数字以找到数组中缺失数字范围的更好方法
const findMissing = num => {
const max = Math.max(...num); // Will find highest number
const min = Math.min(...num); // Will find lowest number
const missing = []
for(let i=min; i<= max; i++) {
if(!num.includes(i)) { // Checking whether i(current value) present in num(argument)
missing.push(i); // Adding numbers which are not in num(argument) array
}
}
return missing;
}
findMissing([1,15]);
答案 4 :(得分:2)
尝试运行代码段
var a = [1,4,7], count = a[a.length - 1];
var missing = [];
for ( var i = 1; i <= count; i++ ) {
if (a.indexOf(i) == -1) {
missing.push(i);
}
}
alert(missing.toString());
答案 5 :(得分:1)
您可以切换循环并设置标记。这可能不是最快的方法,但值得关注,因为这是你的第一个想法。
示例:jsFiddle
var missing = [];
var a = [5];
var count = 5;
var found = false;
for (var j = 1; j < count; j++) {
found = false;
for (var i = 0; i <= a.length; i++) {
if (a[i] == j) {
found = true;
break;
}
}
if (!found) {
missing.push(j);
}
}
alert(JSON.stringify(missing));
答案 6 :(得分:1)
简单
考虑_是lodash或下划线
var arr = [2,3,4,5];
var min = _.min(arr);
var result = [];
for (var i = 0; i < min; i++) {
result.push(i);
}
result // [0, 1];
&#13;
答案 7 :(得分:0)
可以通过找到总数(n *(n + 1)/ 2)并从每个值中减去总数来找到缺少的号码,重命名号码将是所需的号码。
function findNumber(arr) {
var n = arr.length;
var total = ((n + 2) * (n + 1)) / 2;
for (let i = 0; i < arr.length; i++) {
total -= arr[i];
}
}
var arr = [1, 2, 3, 4, 5, 6, 7, 8];
findNumber(arr) // 9
答案 8 :(得分:0)
一种方法是使用此方法查找缺失的数字:
Array.from({length: Math.max(...b)},(_,x) => !b.includes(x+1)?x+1:false).filter(Boolean)
答案 9 :(得分:0)
var a = [100,1,4,7];
a.sort(function(a, b) {
return a - b;
});
count = a[a.length - 1];
var missing = [];
for ( var i = 1; i <= count; i++ ) {
if (a.indexOf(i) == -1) {
missing.push(i);
}
}
console.log(missing.toString());
答案 10 :(得分:0)
var a = [5],
count = 5;
var missing = new Array();
for (var i = 1; i <= count; i++) {
if (a.indexOf(i) == -1) {
missing.push(i);
}
}
console.log(missing); // to check the result.
答案 11 :(得分:0)
Lodash解决方案:
它适用于数组值的最小值和最大值之间缺少的任何数字。
let numbersArray = [0, 1, 3, 6, 7];
let missedNumbersArray = [];
_.forEach(_.range(_.min(numbersArray), _.max(numbersArray)), (number, index) => {
if (_.findIndex(numbersArray, val => val == number) == -1) {
missedNumbersArray.push(index);
}
});
答案 12 :(得分:0)
const myFunc = (arr) => {
const sum = ((arr.length + 1) * (arr.length + 2)) / 2;
const arrSum = (arr) => arr.reduce((a, b) => a + b, 0);
return sum - arrSum(arr);
};
const myArray = [1, 2, 10, 5, 6, 4, 7, 9, 3, 11];
console.log(myFunc(myArray));
说明:
答案 13 :(得分:0)
您可以使用XOR运算符来做到这一点:
let arr1 = [1, 2, 3, 4, 5, 6, 7, 8];
let x1 = xorOfArray(arr1, arr1.length); // 8
console.log(x1);
let arr2 = [1, 2, 3, 4, 5, 7, 8]; // 6 is missing
let x2 = xorOfArray(arr2, arr2.length); // 14
console.log(x2);
let arr3 = [x1, x2];
let x3 = xorOfArray(arr3, arr3.length); // 6
console.log(x3);
// x3 is the missing number
function xorOfArray(arr, n)
{
// Resultant variable
let xor_arr = 0;
// Iterating through every element in
// the array
for (let i = 0; i < n; i++) {
// Find XOR with the result
xor_arr = xor_arr ^ arr[i];
}
// Return the XOR
return xor_arr;
}
答案 14 :(得分:0)
function missingItems(arr, n) {
let missingItems = [];
for (let i = 1; i <= n; i++) if (!arr.includes(i)) missingItems.push(i);
return missingItems;
}
console.log(missingItems([9, 1, 4, 2, 10, 6], 10));
尽管此解决方案的时间复杂度为0(n),但您可以通过运行从1到n的循环并检查每次迭代中数组arr中是否存在索引来轻松获取1到n之间的所有缺失项。
答案 15 :(得分:0)
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const filterArr= [...new Set(A.filter(x=> x>0).sort())];
let k=1;
function newArr(){
for(let i=0;i<filterArr.length;i++){
if(filterArr[i]=== k){
k++;
}
else{
return k;
}
}
return k;
}
return filterArr.length? newArr():1;
}
console.log(solution([3,2,1,5])); output:4
console.log(solution([-1,-2.-5])); output: 1
console.log(solution([1,2.3,4])); output: 5
答案 16 :(得分:0)
这仅适用于有序数组:
const findMissingNumber = (arr) => {
for (let i = 1; i <= arr.length; i++) {
if (arr[i - 1] !== i) {
return i;
}
}
}
const arr = [1, 2, 3, 4, 5, 6, 8, 10];
console.log(findMissingNumber(arr));
答案 17 :(得分:-1)
这是如何在数组(有序数组)中找到缺失的数字,
它只检查一个缺失的数字。
function miss(arr) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== i + 1) {
return i + 1;
}
}
}
let a = miss([1, 2, 3, 4, 5, 7, 8, 9, 10])
console.log(a)
function missing(arr) {
let n = arr.length;
let sum = (n + 1) * (n + 2) / 2
let total = arr.reduce((acc, item) => acc + item);
let missin = sum - total
return missin
}
let b = missing([1, 2, 3, 4, 6, 7, 8, 9]);
console.log(b)