Related
I don't understand this method of pop and unshift in this array
let nums = [1, 2, 3, 6, 9, 8, 7, 4];
const ids = [1, 2, 3, 6, 9, 8, 7, 4];
let btn5 = document.getElementById("btn5");
btn5.onclick = function() {
nums.unshift(nums.pop());
for (i = 0; i <= 7; i++) {
document.getElementById("btn" + ids[i]).innerHTML = nums[i];
}
}
nums.unshift(nums.pop()); is:
// Remove the last entry from the array
const tmp = nums.pop();
// Insert it at the beginning of the array
nums.unshift(tmp);
So for instance, the first time that runs, nums starts with:
[1, 2, 3, 6, 9, 8, 7, 4]
so pop removes the 4 from the end, and inserts it at the beginning:
[4, 1, 2, 3, 6, 9, 8, 7]
Live Example:
const nums = [1, 2, 3, 6, 9, 8, 7, 4];
console.log("before:", JSON.stringify(nums));
nums.unshift(nums.pop());
console.log("after: ", JSON.stringify(nums));
Details on MDN: pop, unshift.
Array pop method removes and return last element of an array. If you write something like,
let nums = [1, 2, 3, 6, 9, 8, 7, 4];
const ids = [1, 2, 3, 6, 9, 8, 7, 4];
const poppedValue = nums.pop(); //poppedValue = 4 and nums = [1, 2, 3, 6, 9, 8, 7]
And unshift method push the item at the beginning of the array.
nums.unshift(poppedValue); // nums = [4, 1, 2, 3, 6, 9, 8, 7];
I am working on a problem on LeetCode and having some troubles
https://leetcode.com/problems/relative-sort-array/
Instructions:
Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.
Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that don't appear in arr2 should be placed at the end of arr1 in ascending order.
Example 1:
Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
Output: [2,2,2,1,4,3,3,9,6,7,19]
my attempt:
var relativeSortArray = function(arr1, arr2) {
let arr =[]
let end =[]
for (i=0; i<arr2.length; i++){
for (j=0; j<arr1.length; j++){
if(arr2[i] == arr1[j]){
arr.push(arr1[j])
}else{
end.push(arr1[j])
}
}
}
end.sort((a,b) => a-b)
console.log(end)
return arr
};
The If conditional works but the else condition isn't and I can't figure out why.
I think console.log(end) should give me the two numbers not in arr2 but it instead gives me:
[
1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 6,
6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 9, 9,
9, 9, 9, 19, 19, 19, 19, 19, 19
]
Why is this happening?
Thanks!!!
You could take an object for the position of a value and take a large value like Number.MAX_VALUE as default value. If the delta is zero sort by value.
Taking a delta is a standard by using Array#sort. This returns a value smaller than zero, zero or greater than zero, depending on the values. The sort method receives this values and keeps or swaps the values.
const
relativeSort = (array, given) => {
const order = Object.fromEntries(given.map((v, i) => [v, i + 1]));
return array.sort((a, b) =>
(order[a] || Number.MAX_VALUE) - (order[b] || Number.MAX_VALUE) ||
a - b
);
};
console.log(...relativeSort([2, 3, 1, 3, 2, 4, 6, 7, 9, 2, 19], [2, 1, 4, 3, 9, 6]));
In each iteration of arr2:
all the numbers that are different from the current number are pushed to the end array
For example,
First iteration - compare number (2) and you will end up with:
arr: [2,2,2]
end: [3,1,3,4,6,7,9,19]
Second iteration - compare number (1) and you will end up with:
arr: [2,2,2,1]
end: [3,1,3,4,6,7,9,19] + [2,3,3,2,4,6,7,9,2,19]
try to debug your code to follow the flow
class Solution:
def relativeSortArray(self, arr1: list[int], arr2: list[int]) -> list[int]:
arr = []
for i in arr2:
value = arr1.count(i)
for j in range(value):
arr.append(i)
arr1.remove(i)
arr1.sort()
return arr+arr1
obj = Solution()
arr1 = [28,6,22,8,44,17]
arr2 = [22,28,8,6]
result = obj.relativeSortArray(arr1,arr2)
print(result)
I have an array, which contains duplicate values. How can I push duplicates in to another array?
let arr1 = [1, 5, 3, 6, 9, 5, 1, 4, 2, 7, 9], and duplicates array should be dupArr = [1, 5, 9]
You could filter the array by storing the previous checked values in a Set, which is here a closure.
var array = [1, 5, 3, 6, 9, 5, 1, 4, 2, 7, 9],
duplicates = array.filter((s => v => s.has(v) || !s.add(v))(new Set));
console.log(duplicates);
For example: I have set 1,3,4,5,6,7,8,10,11
scenario 1 : subset = 1,3,4 which is in starting part of the series = Acceptable
scenario 2 : subset = 8,10,11 which is in ending part of the series = Acceptable
scenario 3 : subset = 1,3,10,11 which is in both starting and ending part of the series = Acceptable
scenario 4 : subset = 1,3,6,8 which is in starting part of series and also in the middle of the series = Not Acceptable.
Main thing to achieve is to check if the given series is in starting part of the series or ending part without breaking the series using jquery or javacript
Help will be much appreciated
Thanks in advance
You could use a two pass approach, with first collecting for all subarray items the comparison at the same index from start and end (later denoted as left and right side).
To indicate a result, 4 states are returned which mean
0 - no match, neither from the left nor from the right side matches the value,
1 - item of the left side of the array matches,
2 - item of the right side of the array matches,
3 - item matches on both sides.
After collecting the values which represent match and position, the values have to be consolidated to the wanted result, true or false.
To check each value, you need another value which represents the expected side. At start, the expected side is set to 1, because it starts at the left side to check.
The temporary result of the callback with Array#every is a single check if the actual value is greater or equal to the expected value. This includes indicator 3 because this value is in both sides. A value of zero exits the loop as well.
If a value of 2 is found, then it indicates the right side and all following elements have to be greater than 2, therefor the expected value is set to 2.
The presented solution could be shortened to just return the wanted boolean value.
function check(array, subarray) {
var expected = 1,
temp = subarray.map(function (a, i) {
var offset = array.length - subarray.length;
return (a === array[i]) + 2 * (a === array[i + offset]);
}),
result = temp.every(function (a) {
var r = a >= expected;
if (a === 2) {
expected = 2;
}
return r;
});
return temp.concat(result);
}
var array = [1, 3, 4, 5, 6, 7, 8, 10, 11];
console.log(check(array, [1, 3, 4])); // true
console.log(check(array, [8, 10, 11])); // true
console.log(check(array, [1, 3, 10, 11])); // true
console.log(check(array, [1, 3, 6, 8])); // false
console.log(check([1, 2, 3, 4, 5, 6], [1, 4, 3, 6])); // false
console.log(check([1, 2, 3, 4, 5, 6], [1, 4, 3, 6])); // false
console.log(check([1, 2, 3, 2, 3, 4], [3, 2, 3, 2])); // false
console.log(check([1, 2, 3, 2, 3, 4], [1, 2, 3, 4])); // true
.as-console-wrapper { max-height: 100% !important; top: 0; }
The short version without a temporary array.
function check(array, subarray) {
var offset = array.length - subarray.length,
expected = 1;
return subarray.every(function (a, i) {
var state = (a === array[i]) + 2 * (a === array[i + offset]),
result = state >= expected;
if (state === 2) {
expected = 2;
}
return result;
});
}
var array = [1, 3, 4, 5, 6, 7, 8, 10, 11];
console.log(check(array, [1, 3, 4])); // true
console.log(check(array, [8, 10, 11])); // true
console.log(check(array, [1, 3, 10, 11])); // true
console.log(check(array, [1, 3, 6, 8])); // false
console.log(check([1, 2, 3, 4, 5, 6], [1, 4, 3, 6])); // false
console.log(check([1, 2, 3, 4, 5, 6], [1, 4, 3, 6])); // false
console.log(check([1, 2, 3, 2, 3, 4], [3, 2, 3, 2])); // false
console.log(check([1, 2, 3, 2, 3, 4], [1, 2, 3, 4])); // true
.as-console-wrapper { max-height: 100% !important; top: 0; }
You would iterate through the subarray from left to right, and from right to left, and stop where the value does not match with the value in the main array at the "corresponding position", i.e. the position counting from the same side of the array.
If at the end of both loops all values of the subarray matched, i.e. the two indexes crossed each other, then the result is true.
Here is the code:
function isSubsetAtEnds(array, subarray) {
const diff = array.length - subarray.length;
let i, j;
if (diff < 0) return false;
for (i = 0; i < subarray.length; i++)
if (array[i] !== subarray[i]) break;
for (j = subarray.length - 1; j >= i; j--)
if (array[j+diff] !== subarray[j]) break;
return j < i;
}
var array = [1, 3, 4, 5, 6, 7, 8, 10, 11];
console.log(isSubsetAtEnds(array, [1, 3, 4])); // true
console.log(isSubsetAtEnds(array, [8, 10, 11])); // true
console.log(isSubsetAtEnds(array, [1, 3, 10, 11])); // true
console.log(isSubsetAtEnds(array, [1, 3, 6, 8])); // false
One simple approach: remove matching numbers from the beginning and end of both arrays, and see if there are any leftovers.
var checkIt = function(arr,subarr) {
// strip off matching numbers at the beginning
while (subarr.length && arr[0] === subarr[0]) {
arr.shift();
subarr.shift();
}
// strip off matching numbers at the end
while (subarr.length && arr[arr.length - 1] === subarr[subarr.length - 1]) {
arr.pop();
subarr.pop();
}
// if there aren't any leftovers, return true
return (subarr.length === 0)
}
console.log(checkIt( [1, 3, 4, 5, 6, 7, 8, 10, 11],[1, 3, 4]));
console.log(checkIt( [1, 3, 4, 5, 6, 7, 8, 10, 11],[8, 10, 11]));
console.log(checkIt( [1, 3, 4, 5, 6, 7, 8, 10, 11],[1, 3, 10, 11]));
console.log(checkIt( [1, 3, 4, 5, 6, 7, 8, 10, 11],[1, 3, 6, 8]));
// pathological edge case:
console.log(checkIt( [1,2,3,2,1],[1,2,3,2,1] ))
// This returns true, but based on the rules of the puzzle I'm honestly not sure whether that's correct.
I was playing around with JavaScript's array prototype map and I don't understand why I'm getting the following values:
console.log(
[1,2,2,5,6].map(function(a,b){
return a+b;
})
); /*This returns [ 1, 3, 4, 8, 10 ]*/
Shouldn't the above code return [1,3,4,7,11] ?
Here's another snippet which shows that elements in the array are added sequentially and are correct at least I believe so.
console.log(
[1,2,3,4,5,6].map(function(a,b){
return a+b;
})
); /*[ 1, 3, 5, 7, 9, 11 ]*/
This is just a curious question more along the lines of why the first snippet of code seems.
It's because map passes you the value as the first parameter and the index as the second. So, what you're adding is: 1 + 0, 2+1, 2+2, 5+3, etc.
Your a value is the value out of the array: 1, 2, 2, 5, 6
Your b value is actually an index, not a value out of the array: 0, 1, 2, 3, 4
Let's sum:
[1, 2, 2, 5, 6] // Your values
+ [0, 1, 2, 3, 4] // Indices
-----------------
[1, 3, 4, 8,10] // Result
[1, 2, 3, 4, 5, 6] // Your values
+ [0, 1, 2, 3, 4, 5] // Indices
--------------------
[1, 3, 5, 7, 9,11] // Result
The results are correct.
I think you are confusing map with reduce:
var arr = [];
[1,2,2,5,6].reduce(function(a,b){
arr.push(a+b);
return b;
}, 0);
arr; // [1, 3, 4, 7, 11]
[0, 1, 2, 2, 5] // Values returned in previous iteration
+ [1, 2, 2, 5, 6] // Your values
-----------------
[1, 3, 4, 7,11] // Result
so the first parameter 'a' is value and the 'b' is index. so adding together it shows the corect value only. so 'a' contains [1, 2, 2, 5, 6] and 'b' contains [0, 1, 2, 3, 4]