Reputation: 263
I'm trying to solve the task, get quantity of missed elements from an array. For example, if given an array [1,3,6], the quantity of missed elements is 3 (2,4,5). But somewhere code goes wrong and system doesn't accept the code. I tried some methods, but unfortunately they are useless.
function arr(x){
let y = [];
for (let i = x[0]; i <= x[x.length-1]; i++){
y.push(i);
}
return y.length-x.length;
}
let m = arr([1,2,3,4,5,6]);
console.log(m);
Or...
function arr(x){
let y = [];
for (let i = 0; i < x.length; i++){
for (let j = 0; j < i; j++){
if (x[i] == x[j]){
x.splice(i,1);
i--;
}
}
}
console.log(x);
for (let i = x[0]; i <= x[x.length-1]; i++){
y.push(i);
}
console.log(y);
return y.length-x.length;
}
let l = arr([1,3,2,4,9]);
console.log(l);
I also tried to sort array, but there are no changes
Upvotes: 2
Views: 160
Reputation: 12796
To be honest, you don't really need a for loop. I think you can calculate the nr by checking the maximum number, the minimum number and the length of the array.
Would this also work for you?
const source = [1,3,6];
/**
* @method nrOfMissedItems
* @param {Array<Number>} an array containing only numbers
* @returns -Infinity when the parameter arr is null or undefined, otherwise number of non-mentioned numbers, ie [5,5] returns 0, [1,1,1,3] returns 1
* When the array contains non-numbers it will return NaN
*/
function nrOfMissedItems( arr ) {
const noDuplicates = [...new Set(arr)];
const highestNumber = Math.max( ...noDuplicates );
const lowestNumber = Math.min( ...noDuplicates );
return highestNumber - lowestNumber - noDuplicates.length + 1;
}
console.log( nrOfMissedItems( source ) ); // 3
console.log( nrOfMissedItems( [1] ) ); // 0
console.log( nrOfMissedItems( [0,1,4] ) ); // 2
console.log( nrOfMissedItems( [5,3,1] ) ); // 2
console.log( nrOfMissedItems( [1,1,1,1,5] ) ); // 3
console.log( nrOfMissedItems( null ) ); // -Infinity
console.log( nrOfMissedItems( undefined ) ); // -Infinity
console.log( nrOfMissedItems() ); // -Infinity
console.log( nrOfMissedItems( ['a','b', 1] ) ); // NaN
console.log( nrOfMissedItems( ['a', null, 1] ) ); // NaN
console.log( nrOfMissedItems( [undefined, 1] ) ); // NaN
Upvotes: 4
Reputation: 36584
You can do that in following steps:
Note: I am considering array is sorted.
function missed(arr){
arr = [...new Set(arr)];
return arr[arr.length - 1] - arr[0] - arr.length + 1
}
console.log(missed([1,3,6]))
If you need to use this on a unsorted array. Then use Math.max()
and Math.min()
.
function missed(arr){
arr = [...new Set(arr)];
let max = Math.max(...arr);
let min = Math.min(...arr);
return max - min - arr.length + 1
}
console.log(missed([1,3,6]))
console.log(missed([6,1,3]))
Upvotes: 2
Reputation: 897
You can try like :
function arr(x){
let y = [];
let start = Math.min(...x);
let end = Math.max(...x);
for (let i = start; i <= end; i++){
y.push(i);
}
return y.length-x.length;
}
let m = arr([1,3,6]);
console.log(m);
Upvotes: 0
Reputation: 11807
I don't know if optimization is a must, bust the easiest non-ES6 (for all browsers support) way I can think of is to sort the array and iterate through it logging the numbers that you expect but are missing. Something like this:
var arr = [4,1,6,8];
arr.sort();
var miss = [];
var expect = arr[0]; // The starting number is the first element in the array I guess. Change to 1 if you want it to be 1
// Loop through the sorted array
for (var i = 0; i < arr.length; i++) {
while(arr[i] > expect++) { // keep looping while the current number is not the one expected, and increment the expected by one
miss.push(expect-1); // If missed, record the missed number (which is the actual expected minus 1, as we already incremented it)
}
}
console.log(miss);
Upvotes: 0