Check if the array has three consecutive numbers in sequence [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
i have an array like below
[1,2,'b',4 ,'a','b',5,'o',7,1,3,'p',9,'p']
I want to check that if the above array has three consecutive numbers in a sequence (i.e) [1,2,3].
From the above array i want the output as below given example
[7,1,3] - > Since this sequence is occuring in a sequence without getting blocked by a alphabet.

You could take a temporary array and fill it with found numbers. For any not found number, empty the array and check then the length. If it has the wanted length, push the array to the result set.
var array = [1, 2, 'b', 4, 'a', 'b', 5, 'o', 7, 1, 3, 'p', 9, 'p'],
three = array.reduce((temp => (r, v) => {
if (typeof v !== 'number') {
temp = [];
return r;
}
temp.push(v);
if (temp.length === 3) {
r.push(temp);
}
return r;
})([]), []);
console.log(three);

Here is a way to do this . Just iterate over array and count number of occurance of numbers. As soon as count becomes 3 you have your magic numbers otherwise reset all. As #Rup suggested.
var arr = [1,2,'b',4 ,'a','b',5,'o',7,1,3,'p',9,'p'];
var result = [];
var count = 0;
arr.forEach(function(element) {
if(typeof (element) == "number" ){
count +=1;
result.push(element)
}else {
if(count == 3){
console.log(result);
return ;
}else if(count < 3){
count = 0;
result= [];
}
}
});

Related

How to count one array with respect to another Array [closed]

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 2 years ago.
Improve this question
Suppose I have two arrays
parentArry = [1,2,3]
ChildArr = [1,2,3,4,5,6]
I want to pick to element of child array from each counter of parent array.
something like this :
1 - 1,2
2 - 3,4
3 - 5,6
Here what I am trying but it is not working
parentArry = [1,2,3]
ChildArr = [1,2,3,4,5]
for(var i=0; i<parentArry.length; i++){
console.log(ChildArr[i] + "And" + ChildArr[i+1])
}
I believe this is what you're looking for to get your expected output:
parentArry = [1,2,3]
ChildArr = [1,2,3,4,5,6]
for(var i=0; i<parentArry.length; i++){
console.log(ChildArr[i*2] + "And" + ChildArr[i*2+1])
}
What I believe you're looking for is n windows of size m where n is given by the parent array and you are generating windows from the child array.
This question is somewhat vague, but if you must approach this problem using these two arrays as such, you could do something like:
const a = [1, 2, 3];
const b = [1, 2, 3, 4, 5];
let first;
let second;
wsize = 2
for (var i = 0; i < a.length + wsize; i = i + wsize) {
first = b[i]; // could be undefined
second = b[i + 1]; // could also be undefined
if (first !== undefined && second !== undefined) {
console.log(b[i] + " and " + b[i + 1])
} else if (first !== undefined) {
console.log(b[i])
} else break
}
This is clearly an instructional answer. It is verbose and not very elegant. You'll need to be aware that the length of the first array may result in the production of undefined for the values of first and second If the length of the child array is not divisible by length of the parent array or if the parent array is longer than the child array).
There are better solutions for windowing as well which are flexible enough to handle arbitrary window sizes and arbitrary length child arrays.
What would a more elegant solution look like?
const a = [1, 2, 3];
const b = [1, 2, 3, 4, 5];
const wsize = 2;
for (var i = 0; i < a.length + wsize; i = i + wsize) {
console.log(b.slice(i, i + wsize).join(" , "))
}
Here is my guess - it is long winded but can handle the 5 items which were present when I started coding
const parentArry = [1, 2, 3]
const childArr = [1, 2, 3, 4, 5]
let cnt = 0;
parentArry.forEach(item => {
const res = [childArr[cnt]], next = childArr[++cnt];
if (next) res.push(next);
console.log(item,"-",res.join(","));
cnt++;
})

Given an array of unique numbers, list the combination of numbers which sum equals to given integer [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Input
Array =[4,2,8,11,14,6,1]
Expectedtotal=20
Output
[2,4,6,8], [11,8,1],[6,11,1,2],[2,14,4],[14,6]
code:
const arr = [4, 2, 8, 11, 14, 6, 1];
const target = 20;
res = _.filter(arr, v => _.filter(arr, v1 => _.filter(arr, v2 => _.filter(arr, v3 => v + v1 + v2 + v3 === target))); console.log(res);
I am the beginner of javascript and lodash. I tried this code by own..please help me to write good code.
You could use a temporary array and the index and iterate the next index by either taking the actual value or not.
function subsetSum(array, sum) {
function iter(index, temp, s) {
if (s === sum) return result.push(temp.slice()); // exit sum found
if (index >= array.length) return; // exit index over
iter(index + 1, temp.concat(array[index]), s + array[index]); // take value
iter(index + 1, temp, s); // omit value
}
var result = [];
iter(0, [], 0);
return result;
}
console.log(subsetSum([4, 2, 8, 11, 14, 6, 1], 20).map(a => a.join(' ')));

How to compare one array with another in javascript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have two arrays as below:
var arr1 = [1,2,3,8,7,5,7,2,9,0];
var arr2 = [8,7,5];
I want to compare arr2 with arr1, when it will find arr2 exactly in the same sequence as it is then it should return true. i.e. if [8,7,5] is found exactly same sequence in arr1 then it will return true.
Note:
We have to do this without using indexOf.
You could use a combination of Array#some and Array#every.
var array1 = [1, 2, 3, 8, 7, 5, 7, 2, 9, 0],
array2 = [8, 7, 5],
result = array1.some(function (a, i, aa) {
return array2.every(function (b, j) {
return aa[i + j] === b;
});
});
console.log(result);
You can loop through the largest array. On each iteration, compare the next values to all of the values found in the smaller array. If they all match, then it contains the smaller array.
var arr1 = [1,2,3,8,7,5,7,2,9,0];
var arr2 = [8,7,5];
console.log(doesArrayContain(arr2, arr1));
function doesArrayContain(smallestArray, biggestArray) {
for (var i = 0; i < biggestArray.length; i++) {
var doesMatch = true;
for (var j = 0; j < smallestArray.length; j++) {
if (biggestArray[i + j] !== smallestArray[j]) {
doesMatch = false; break;
}
}
if (doesMatch) {
return true;
}
}
return false;
}

How to find the smallest numbers in a json file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
What is the best way to loop through a json object that contains integer values and select the smallest values?
For example, if I had an object that looks like this
var z =
{"a": 4,
"b":2,
"c":5,
"d":1,
"e":3
}
And I wanted to pick out the 3 smallest numbers - 1,2,3 in this case- what's the best approach?
You could try the following script:
// create an array to store the values.
var numbers = [];
// loop through the keys of z and push each value in the numbers array.
for(var key in z){
numbers.push(z[key]);
}
// sort the array.
numbers = numbers.sort(function(a,b){ return a-b; });
// pick up the first three.
firstThree = numbers.slice(0,3);
var z =
{"a": 4,
"b":2,
"c":5,
"d":1,
"e":3
}
var numbers = [];
for(var key in z){
numbers.push(z[key]);
}
numbers = numbers.sort(function(a,b){ return a-b; });
firstThree = numbers.slice(0,3);
alert(firstThree)
Get object values into an array using for...in loop. Then sort it using sort() and get the values
Update : You can get the first 3 values using splice()
var z = {
"a": 4,
"b": 2,
"c": 5,
"d": 1,
"e": 3
},
arr = [];
// array for storing values
for (var o in z)
// iterate over the array
arr.push(z[o]);
// push value to the array
document.write(arr
.sort()
// sorting the value array
.splice(0, 3)
// get first three values
.join()
// joining the 3 values
)
I suggest to iterate over the keys of the object and use the keys for reducing the values to the smallest value.
var z = {
"a": 4,
"b": 2,
"c": 5,
"d": 1,
"e": 3
},
smallest = Object.keys(z).reduce(function (r, k) {
return Math.min(r, z[k]);
}, Number.MAX_VALUE);
document.write(smallest);

Max common array slice in javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have an array of numbers and I need to find the maximum slice of the array which contains no more than two different numbers.
so if I have
[1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2, 1, 8]
the output I'm looking is 10, because the array slice of (0, 9) is the largest slice of the array with no more than two different numbers.
How do I achieve that?
This example does the trick for you. However I kindly ask other higher programming gods to improve this or provide another solution. I think this code could be greatly optimized. Please comment if you find bugs, or examples to which this function returns a faulty solution.
function returnLargestConsecutiveArraySlice(array)
{
//set an empty array.
var a = [];
//walk the supplied array and check if number is not repeated
array.filter(function (element, index, array) {
if (element != array[index-1] && element != array[index+1])
{
a.push(index);
return element;
}
}
);
//the returned array contains all indexes to the numbers that are not repeated.
//walk the array and check if number the next number matches the current one.
//If so the index is consecutive.
var numbers = a;
var b = [[]]; //use an empty array to start with.
var index = 0;
for (var i = 0; i < numbers.length-1; i++){
if(numbers[i+1] == numbers[i]+1)
{
//number is consecutive, add.
b[index].push(numbers[i]);
}
else
{
//chain broken, start a new one.
index++;
b[index] = [];
}
}
//we now have an array with all chains. Look for the largest chain.
var largest = [];
for (var i = 0; i < b.length; i++)
{
if (b[i].length > largest.length)
{
largest = b[i];
}
}
//largest chain found. Slice the original array on the largest chain.
return array.slice(largest[0], largest[0] + largest.length+1);
}
console.log(returnLargestConsecutiveArraySlice([1, 1, 1, 2, 2, 2, 4, 5, 6, 1, 1, 7, 8, 9, 10, 11, 2, 2, 6, 2, 1, 8]));

Categories