Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am trying to wrap my head around the following:
We have the following example array: var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
We have user input N as a parameter.
For the sake of this example, let's assume that N = 3.
After that, let's define the var blockSize = 2 * N
The example array arr then has to be split into chunks like these:
[0, 0, 0, 1, 2, 3], [1, 2, 3, 4, 5, 6], [4, 5, 6, 7, 8, 9], [7, 8, 9, 10, 11, 12], [10, 11, 12, 0, 0 ,0]
Note that this is a perfect example where numbers get split perfectly because of the parameter we gave. I need this to be working even with cases, where it won't be so perfect. In that case, zeros should be added to the end of the last chunk to have it properly sized (N*2).
You can slice in a loop:
function overlappingChunks(arr, len) {
const chunks = [];
for (let i = 0; i < arr.length; i += len) {
const chunk = arr.slice(i, i + len * 2);
chunks.push(chunk.concat(new Array(len * 2 - chunk.length).fill(0)));
}
return chunks;
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 months ago.
Improve this question
I ran the code using different arrays missing a number between 1 - 9, but it kept returning -1.
function findMissing(arrOfNumbers) {
const integers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for (let index = 0; index < arrOfNumbers.length; index++) {
if (integers[index] !== arrOfNumbers[index]) {
return integers[index];
}
return -1;
}
}
console.log(findMissing([0, 1, 3, 4, 6, 7, 8, 9])); //returns -1 instead of 5
If I understood the problem, you want missing elements from array 2. if so, simply use,
arr1.filter(x=> !arr2.includes(x));
In your case
const array1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const array2 = [0, 1, 3, 4, 6, 7, 8, 9];
const result = array1.filter(x=> !array2.includes(x)); // returns 2 , 5
console.log(result)
You can spread the array over an object and compare the key and value pairs of the object. If key and value are different then that means some value is missing in the input array that is out of range. Note the use of !=, it is intentional to compare values and not types.
function findMissing(arrOfNumbers) {
const obj = {...arrOfNumbers};
for(const [key, val] of Object.entries(obj)) {
if(key != val) {
return key;
}
}
return -1;
}
// returns 2 since it is the first missing value in the order
console.log(findMissing([0, 1, 3, 4, 6, 7, 8, 9]));
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
The task is to write a function that finds a number of items appearing more than once in an array.
For example:
countIdentic([3, 3, 7, 7, 3, 3, 4, 5, 5, 8, 8, 8]) returns 4
countIdentic([15,14,13,19,13,14,14,14,7,9,9]) returns 3
I already have a solution but I find it complicated. So maybe there is a more elegant solution to that task?
function countIdentic(arr) {
var clone = arr.slice(0),
test = [],
cur,
count = 0;
while (clone.length) {
cur = clone.shift();
if (test.indexOf(cur) === -1) {
test.push(cur);
if (clone.indexOf(cur) >= 0) {
count++;
}
}
}
return count;
}
console.log( countIdentic([3, 3, 7, 7, 3, 3, 4, 5, 5, 8, 8, 8]) );
console.log( countIdentic([15,14,13,19,13,14,14,14,7,9,9]) );
You can use Set, so you don't iterate many times over the original array. But that will take some more space complexity :)
Space complexity: O(n), where n is arr.length. Since seenOnce and duplicates can grow up to arr.length size.
Time complexity: O(n), where n is arr.length. Since we iterate over the arr only once.
function countIdentic(arr) {
const seenOnce = new Set();
const duplicates = new Set();
arr.forEach((item) => {
if (duplicates.has(item)) {
return;
}
if (seenOnce.has(item)) {
duplicates.add(item);
seenOnce.delete(item);
return;
}
seenOnce.add(item);
});
return duplicates.size;
}
console.log(countIdentic([3, 3, 7, 7, 3, 3, 4, 5, 5, 8, 8, 8]));
console.log(countIdentic([15, 14, 13, 19, 13, 14, 14, 14, 7, 9, 9]));
function countIdentic(arr) {
return [... // get number of occurences for every item
arr.reduce((map, num) => map.set(num, (map.get(num) ?? 0) + 1), new Map)
.entries()
]
// filter pairs with more than one occurence
.filter(([num, count]) => count > 1)
// get the count
.length;
}
console.log( countIdentic([3, 3, 7, 7, 3, 3, 4, 5, 5, 8, 8, 8]) );
console.log( countIdentic([15,14,13,19,13,14,14,14,7,9,9]) );
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
This seems like a very straightforward problem but for the life of me, I can not get it sorted.
I want to compare two arrays and if the value of array2 is in array1 push it to a new array otherwise push a 0.
these are my arrays:
let arrayOne = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18];
let arrayTwo = [3, 4, 5, 8, 9, 10, 12, 13, 14, 15, 16];
desired outcome is
[0,0,3,4,5,0,0,8,9,10,11,12,13,14,15,16,0,0]
current attempt with obvious indices issues
let newArray = [];
for (let i=0; i < arrayOne.length; i++){
if(arrayTwo.includes(i)) newArray.push(arrayTwo[i]);
else newArray.push(0);
};
current result
[0, 0, 0, 8, 9, 10, 0, 0, 13, 14, 15, 16, undefined, undefined, undefined, undefined, undefined, 0]
Issue with your approach is that you are searching for an index in arrayTwo
if(arrayTwo.includes(i)) newArray.push(arrayTwo[i]);
where i is the index not a value. That's why you are getting undefined values, because arrayTwo has less values then arrayOne, so index of arrayOne will not exists in arrayTwo.
Instead you should search for value in arrayTwo
for (let i=0; i < arrayOne.length; i++){
let value = arrayOne[i];
if(arrayTwo.includes(value)) newArray.push(value);
else newArray.push(0);
};
If you check only for existence of the value in collection - use Set which designed exactly for this purpose.
let arrayOne = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18];
let values = new Set([3, 4, 5, 8, 9, 10, 12, 13, 14, 15, 16]);
const result = arrayOne.map(value => values.has(value) ? value : 0);
// => [0,0,3,4,5,0,0,8,9,10,11,12,13,14,15,16,0,0]
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have 4 arrays:
Note: Length of array length is different in any session
var Class_A = [8, 11, 3, 9, 11, 16, 16];
var Class_B = [2, 6, 12, 13, 20, 22, 33, 40, 50, 66, 77]; // Greatest length with 11 items,
var Class_C = [10, 14, 19];
var Class_D = [8, 2];
I need to select array with greatest number of items, (Class_B)
My expectation is:
var major = number major of array.length (in this case Class_B);
My code:
var major = max(Class_A.length > Class_B.length > Class_C.length > Class_D.length);
Edit
An unorthodox solution, from a newbie but it works.
var Class_A = [8, 11, 3, 9, 11, 16, 54, 14, 5];
var Class_B = [2, 6, 12, 13, 20, 22, 33, 40, 50, 66, 77, 16, 7]; // Greatest length with 11 items,
var Class_C = [10, 14, 19];
var Class_D = [8, 2];
var Classes = [];
Classes.push(Class_A.length) ;
Classes.push(Class_B.length) ;
Classes.push(Class_C.length) ;
Classes.push(Class_D.length) ;
console.log("Lenght of 4 arrays>"+Classes);
Classes.sort(function(a, b) { // order by major number
return a - b;
});
Classes.reverse(); // reverse array
console.log("Total>"+Classes); // print sort by major number
console.log("Major>"+Classes[0]); // Works => the magic number that I need
Put all your arrays in an array.
classes = [Class_A, Class_B, Class_C, Class_D];
Then, to get the largest one:
classes.reduce((acc, curr) => curr.length > acc.length ? curr : acc);
// => [2, 6, 12, 13, 20, 22, 33, 40, 50, 66, 77]
EDIT:
Or, if you want length of the largest array:
classes.reduce((acc, curr) => curr.length > acc ? curr.length : acc, 0);
// => 11
I have this code here that I've set up:
var numbers = [1,2,3,4,5,6,7,8,9,2,3,4,5,6,1,2,3,4,5,6,7,8,8,4,3,2];
var greaterThan3 = numbers.filter (item => item >= 3);
console.log(greaterThan3);
greaterThan3.reduce((item, amount) => item + amount);
but it only gives me this:
[3, 4, 5, 6, 7, 8, 9, 3, 4, 5, 6, 3, 4, 5, 6, 7, 8, 8, 4, 3]
And, I know it probably has something to do with how I set up the .reduce() line. I'm trying to get the numbers greater than or equal to 3 to add up to one number. I'm assuming I have to define total and amount but I'm not sure. Just learning this chapter from Bloc so excuse the probably dumb question.