Array Difference, nested For Loops - javascript

function arrayDiff(a, b) {
let result = [];
for (let i = 0; i < a.length; i++) {
for (let j = 0; j < b.length; j++) {
if (a[i] !== b[j]) {
result.push(a[i]);
}
}
}
return result;
}
console.log(arrayDiff([1,2,2,3], [1])); // output: [2,2,3]
console.log(arrayDiff([1,2,2,3], [1,2])); // output: [1,2,2,3,3] // desired output: [3]
Trying to solve Array Difference, multiple items inside 'b' causing unwanted output.

Consider using a Set and Array#filter.
const
arr1 = [1, 5, 3, 7, 9],
arr2 = [5, 1, 10, 13],
s = new Set(arr2),
res = arr1.filter((a) => !s.has(a));
console.log(res);

Related

JS for loop in for loop, problem with scope I guess

The input is an array ints [11, 2, 7, 8, 4, 6] and and integer s 10. The function is to output an array with a pair of two numbers from ints which first form a sum of 10. So here the output should be [2, 8], because 2 + 8 = 10. Why does it output empty array? The arrResults was updated in the nested for loop, so why doesn't it show up like that after the final return statement?
function sumPairs(ints, s) {
let arrResults = [];
let sumOfTwo;
for (i = 0; i < ints.length; i++) {
for (j = 0; j < ints.length; j++) {
sumOfTwo = ints[i] + ints[j];
if (sumOfTwo === s) {
arrResults.push(ints[i]);
arrResults.push(ints[j]);
break;
}
}
if (arrResults !== []) {
break;
}
}
return arrResults;
}
console.log(sumPairs([11, 2, 7, 8, 4, 6], 10));
Beside the wrong comparing of an array with another array (without having the same object reference)
a = []
b = []
a === b // false
// other example
a = []
b = a
a === b // true
for checking the length,
a = []
a.length // 0
and by using a nearly quadratic time complexity of n², even with looping
i = 0; i < array.length - 1
j = i + 1; j < array.length
which is more then the half of n², but strill quadratic,
you could take a single loop with an object fo already seen values.
This approach finds the first pair of the array for a certain sum.
function sumPairs(ints, s) {
const needed = {};
for (const value of ints) {
if (needed[value]) return [s - value, value];
needed[s - value] = true;
}
}
console.log(sumPairs([11, 2, 7, 8, 4, 6], 10));
Your code fails because you are checking to see if the array is empty. The problem is that check is never going to be false, so it exits on the first iteration.
console.log([]===[]);
console.log([]!==[]);
So code with changes to improve performance and to exit out
function sumPairs(ints, s) {
let arrResults = [];
let sumOfTwo;
for (let i = 0; i < ints.length; i++) {
for (let j = i + 1; j < ints.length; j++) {
sumOfTwo = ints[i] + ints[j];
if (sumOfTwo === s) {
arrResults.push(ints[i]);
arrResults.push(ints[j]);
break;
}
}
if (arrResults.length) {
break;
}
}
return arrResults;
}
console.log(sumPairs([11, 2, 7, 8, 4, 6], 10));
There is no need to break out twice, just return the array
function sumPairs(ints, s) {
let arrResults = [];
let sumOfTwo;
for (let i = 0; i < ints.length; i++) {
for (let j = i + 1; j < ints.length; j++) {
sumOfTwo = ints[i] + ints[j];
if (sumOfTwo === s) {
return [ints[i], ints[j]];
}
}
}
return null;
}
console.log(sumPairs([11, 2, 7, 8, 4, 6], 10));

Add pattern to an array without push()

Is there a way to code this without using any methods?
a is an array, n is the number of times the pattern is repeated in the new array
const func = (a, n) => {
const arr = [];
for (let i = 0; i < n; i++) {
arr.push(...a);
}
console.log(arr);
};
func([1, 2, 3, 4], 2);
You could take a separate index and assign the values.
const func = (a, n) => {
let array = [],
i = 0;
while (n--) for (const v of a) array[i++] = v;
return array;
};
console.log(...func([1, 2, 3, 4], 2));
Well, you could use two loops and directly assign ith array item.
const func = (a, n) => {
const arr = [];
for (let i = 0; i < n; i++) {
for (let j = 0; j < a.length; j++) {
arr[i * a.length + j] = a[j]
}
}
console.log(arr);
};
func([1, 2, 3, 4], 2);
that ?
const func=(a, n)=>
{
const arr = []
let p = 0
for (let i=0;i<n;++i) for(let v of a) arr[p++] = v
console.log(JSON.stringify(arr));
}
func([1, 2, 3, 4], 2);
You could use the spread operator to create a new array for each iteration of the loop. This would mean that your arr variable can't be a constant though since it gets overwritten with a new array.
const func = (a, n) => {
let arr = [];
for (let i = 0; i < n; i++) {
arr = [...arr, ...a];
}
console.log(arr);
};
func([1, 2, 3, 4], 2);

Iterate over shallow copy of nested array in Javascript

I have a nested array:
let array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
I need to iterate over the first and second element of every nested array and update the changes on the original array. How do I achieve this? I have tried many options but the results don't update the original array. For example:
let arrayCop = [];
for (let i = 0; i <= 1; i++) {
for (let j = 0; j <= 1; j++) {
arrayCop.push(array[i][j]);
}
}
arrayCop.forEach(...);
Thanks.
This is my full code, I'm trying to build a legal sudoku generator:
let sudoku = [];
function populateSudoku() {
let array = [];
while (array.length <= 8) {
let randomNum = Math.floor(Math.random() * 9 + 1);
array.push(randomNum);
if (array.indexOf(randomNum) < array.lastIndexOf(randomNum)) {
array.pop()
}
}
return array;
}
while (sudoku.length <= 8) {
sudoku.push(populateSudoku());
}
for (let i = 0; i < sudoku.length; i++) {
for (let j = 0; j < sudoku.length; j++) {
sudoku[i].forEach(element => {
if (sudoku[i].indexOf(element) === sudoku[j].indexOf(element) &&
(i !== j)) {
sudoku[j][sudoku[i].indexOf(element)] = 0;
}
})
}
}
let array = [];
for (let i = 0; i <= 2; i++) {
for (let j = 0; j <= 2; j++) {
array.push(sudoku[i][j]);
}
}
array[3] = 452345;
console.log(sudoku);
**
# I did it! #
**
let array = [[1, 2, 3], [7, 4, 1], [2, 4, 3]];
// checks for duplicates just in first and second item of every file
for (let i = 0; i <= 1; i++) {
for (let j = 0; j <= 2; j++) {
array[i].forEach((element, index) => {
if ((i !== j) && index <= 1 &&
(array[j].indexOf(element) >= 0 && array[j].indexOf(element) <= 1)) {
array[i][index] = 'x';
}
})
}
}
console.log(array);
If I understand right, you would like to change the original array to:
[[1, 2], [4, 5], [7, 8]]
If so, this would do it:
array.forEach(element => element.splice(2))
You can use Array.prototype.map function
ORIGINAL
I need to iterate over the first and second element of every nested
array and update the changes on the original array
function iterate(array) {
array.forEach(function(element, index) {
console.log('[' + index + "][0]", element[0]);
console.log('[' + index + "][1]", element[1])
})
}
Not sure what you mean by update changes to the original array, though...
EDIT
Alright, after looking through other answers, I believe #NinaW got what you were looking for.
function parse(array) {
array.forEach(function(element) { element.slice(0, 2) })
}
Use flatMap and destructuring.
let array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
let arrayCop = array.flatMap(([first, second]) => [first, second]);
console.log(arrayCop)
let array = [[1, 2, 3], [7, 4, 1], [2, 4, 3]];
console.log(array);
// checks for duplicates just in first and second item of every file
for (let i = 0; i <= 1; i++) {
for (let j = 0; j <= 2; j++) {
array[i].forEach((element, index) => {
if ((i !== j) && index <= 1 &&
(array[j].indexOf(element) >= 0 && array[j].indexOf(element) <= 1)) {
array[i][index] = 'x';
}
})
}
}
console.log(array);

How to get all substrings (contiguous subsequences) of my JavaScript array?

My task is to split the given array into smaller arrays using JavaScript. For example [1, 2, 3, 4] should be split to [1] [1, 2] [1, 2, 3] [1, 2, 3, 4] [2] [2, 3] [2, 3, 4] [3] [3, 4] [4].
I am using this code:
let arr = [1, 2, 3, 4];
for (let i = 1; i <= arr.length; i++) {
let a = [];
for (let j = 0; j < arr.length; j++) {
a.push(arr[j]);
if (a.length === i) {
break;
}
}
console.log(a);
}
And I get the following result: [1] [1, 2] [1, 2, 3] [1, 2, 3, 4] undefined
What am I missing/doing wrong?
For the inner array, you could just start with the index of the outer array.
var array = [1, 2, 3, 4],
i, j, l = array.length,
result = [];
for (i = 0; i < l; i++) {
for (j = i; j < l; j++) {
result.push(array.slice(i, j + 1));
}
}
console.log(result.map(a => a.join(' ')));
.as-console-wrapper { max-height: 100% !important; top: 0; }
You have two issues in your code:
You need to have loop to initialize with the value of i for the inner loop so that it consider the next index for new iteration of i
You need to remove that break on the length which you have in inner loop.
let arr = [1, 2, 3, 4];
for (let i = 0; i <= arr.length; i++) {
let a = [];
for (let j = i; j < arr.length; j++) {
a.push(arr[j]);
console.log(a);
}
}
Try this
let arr = [1, 2, 3, 4];
for (let i = 0; i <= arr.length; i++) {
let a = [];
for (let j = i; j < arr.length; j++) {
a.push(arr[j]);
console.log(a);
}
}
If you don't want to mutate your array.
let arr = [1, 2, 3, 4];
let res = [];
for (let i = 0; i <= arr.length; i++) {
let a = [];
for (let j = i; j < arr.length; j++) {
a = [...a, arr[j]];
res = [...res, a];
}
}
console.log(res);
i have prepare stackblitz for this case.
let source = [1,2,3,4];
const output = [];
const arrayMultiplier = (source) => {
const eachValueArray = [];
source.forEach((item, index) => {
// Will push new array who will be sliced source array.
eachValueArray.push(source.slice(0, source.length - index));
});
//We reverse array to have right order.
return eachValueArray.reverse();
};
for(let i = 0; i <= source.length; i++) {
output.push(...arrayMultiplier(source));
source.shift(); // Will recraft source array by removing first index.
}
//Don't forget last item.
output.push(source);
console.log(output);
Is not the most shorten solution but do the job
== update after code review ==
// [...]
const arrayMultiplier = (source) => {
// Will push new array who will be sliced source array.
// We reverse array to have right order.
return source.map((item, index) => source.slice(0, source.length - index)).reverse();
};
// [...]
Use two iteration
get slice array based on loop index.
use sliced array and combine array element.
var arr = [1, 2, 3, 4];
let newArra =[];
arr.map((x,i)=> {
let remainArr = arr.slice(i);
return remainArr.forEach((y, r) => newArra.push(remainArr.slice(0, r+1)))
})
newArra.forEach(x=> console.log(x))

What is the difference between [4,null,6] and [4,6]

Here is the question:
Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.
And here is my code:
function diffArray(arr1, arr2) {
var newArr = [];
// Same, same; but different.
for (i = 0; i < arr1.length; i++) {
for (j = 0; j < arr2.length; j++)
while (arr1[i] === arr2[j])
delete arr2[j];
newArr = arr2;
}
return newArr;
}
console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));
Please, tell me my mistakes.
If you work using indices as references which you delete, you'll leave those indices undefined.
You have to use push to add an item and splice to remove one.
The time complexity of the following code should be: O(nm) where n and m are the lengths of the arr1 and arr2 arrays respectively.
function diffArray(arr1, arr2) {
var newArr = [];
for (i = 0; i < arr1.length; i++) {
for (j = 0; j < arr2.length; j++)
while (arr1[i] === arr2[j])
arr2.splice(j, 1);
newArr = arr2;
}
return newArr;
}
console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));
This should work, but I've found a different way that is a bit slower for short arrays but much faster for longer array.
The time complexity of the following code should be: O(3(n + m)), which is reduced to O(n + m) where n and m are the lengths of the arr1 and arr2 arrays respectively.
Look at this fiddle.
Here's it:
function diffArray(arr1, arr2) {
let obj1 = {}, obj2 = {};
for (let l = arr1.length, i = 0; i < l; i++)
obj1[arr1[i]] = undefined;
for (let l = arr2.length, i = 0; i < l; i++)
obj2[arr2[i]] = undefined;
let a = [];
for (let arr = arr1.concat(arr2), l = arr.length, i = 0, item = arr[0]; i < l; i++, item = arr[i])
if (item in obj1 !== item in obj2)
a.push(item);
return a;
}
console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));
The task you are trying to complete asks you to create a new array, but instead you modify arr2. It would probably be easiest to just copy all elements not included in the other array to a new array, like this:
function diffArray(arr1, arr2) {
var newArray = [];
arr1.forEach(function(el) {
if (!arr2.includes(el)) {
newArray.push(el);
}
});
arr2.forEach(function(el) {
if (!arr1.includes(el)) {
newArray.push(el);
}
});
return newArray;
}
If you would rather try and fix your code instead, I can try to have another look at it.
I've used Array.prototype.filter method:
function diffArray(arr1, arr2) {
var dif01 = arr1.filter(function (t) {
return arr2.indexOf(t) === -1;
});
var dif02 = arr2.filter(function (t) {
return arr1.indexOf(t) === -1;
});
return (dif01).concat(dif02);
}
alert(diffArray([1, 2, 3, 6, 5], [1, 2, 3, 4, 7, 5]));
If you still want to use your code and delete the common elements, try to use Array.prototype.splice method instead of delete: the latter deletes the value, but keeps the index empty, while Array.prototype.splice will remove those whole indices within the given range and will reindex the items next to the range.
You can use Array.prototype.filter:
var array1 = [1, 2, 3, 5];
var array2 = [1, 2, 3, 4, 5];
var filteredArray = filter(array1, array2).concat(filter(array2, array1));
function filter(arr1, arr2) {
return arr1.filter(function(el) { return arr2.indexOf(el) < 0; });
}
Here is a working JSFiddle.
Try this:
function diffArray(arr1, arr2) {
var ret = [];
function checkElem(arrFrom, arrIn) {
for (var i = 0; i < arrFrom.length; ++i) {
var elem = arrFrom[i];
if (arrIn.indexOf(elem) === -1)
ret.push(elem);
}
}
checkElem(arr1, arr2);
checkElem(arr2, arr1);
return ret;
}
I hope this can solve your problem.

Categories

Resources