A sub-array is a subset of an array.
Questions tagged [sub-array]
434 questions
1
vote
2 answers
Ruby - elegantly flatten an array but don't ignore empty sub-arrays
Using .flatten is a handy little trick to take an array of sub-arrays and turn it into a single array.
For example: [[1,3],2,[5,8]].flatten => [1,3,2,5,8]
You can even include nil [1,[2,nil],3].flatten will result in [1,2,nil,3].
This kind of method…
user7600286
1
vote
2 answers
Number of times sub-array is repeated in array - JavaScript
I have a sub-array and i am trying to find to the number of times it is repeated in the main array.
I got to the point where i can determine if it is a sub-array but cannot take it further. Any ideas how i can do that in JavaScript.
function…

Mahima
- 65
- 1
- 8
1
vote
2 answers
To find sum of all consecutive sub-array of length k in a given array
I want to find out all the sum of continuous sub-array of length K
for a given array of Length n given that k < n. For example, let the given array be arr[6]={1,2,3,4,5,6} and k=3,then answer is (6,9,12,15).
It can be obtained as…

Enigma
- 179
- 2
- 12
1
vote
1 answer
MO's Algorithm to find number of elements present in both array
I have 2 arrays, before[N+1](1 indexed) and after[] (subarray of before[]). Now for M Queries, I need to find how many elements of after[] are present in before[] for the given range l,r.
For example:
N = 5
Before: (2, 1, 3, 4, 5)
After: (1, 3, 4,…

Buckster
- 41
- 12
1
vote
1 answer
Sorting sub-arrays in Neo4j
I need to remove all duplicates from a set of arrays, but we define 'duplicate' in a special way here: Two 4 element arrays are 'dupes' if they share the first two elements in any order and the last two elements in any order.
So my thought is to…

mojo2go
- 107
- 10
1
vote
4 answers
PHP - sort nth-level subarray
Let's suppose I've a nested PHP array $aaa where
the entry $aaa[$bbb][$ccc] is like
array(0 => array('x' => 3, 'y' => 2), 1 => array('x' => 2, 'y' => 1), 2 => array('x' => 4, 'y' => 1))
and let's say I want to order just this array by x value in…

user1403546
- 1,680
- 4
- 22
- 43
1
vote
2 answers
How to store the sum of every contigous subarray of an array?
I have an array on n elements (1 <= n <= 200000). I' am suppose to find sum of every contigous subarray that can be formed from this array. I have an O(n^2) algorithm that will find all sums, but my problem is that i cant store it in any data…

Midhun
- 744
- 2
- 15
- 31
1
vote
6 answers
How can I check the size of an array containing sub arrays?
I want something along the lines of array1.length from the below array:
var array1 = {
sub1: [1,2,3],
sub2: ["K","J","H"],
sub3: ["Mango","Armada","Leffen","Mew2king"],
sub4: ['1/8"', '3/16"', '1/4"'],
};
Right now I'm getting the following…

Matthew Sirkin
- 93
- 2
- 14
1
vote
1 answer
identify recurring/duplicate patterns as sub-arrays from a parent array
I have a typical pattern searching problem where I need to identify where multiple patterns are appearing within an array and single them out.
ex: ['horse', 'camel', 'horse', 'camel', 'tiger', 'horse', 'camel', 'horse', 'camel']
function should…

Nishutosh Sharma
- 1,926
- 2
- 24
- 39
1
vote
0 answers
group array into subarrays by part of key in php
I have array from post like this:
$post = array (
'cib_1_34b2e5386658' => 'value1',
'cib_2_34b2e5386658' => 'value2',
'cib_3_34b2e5386658' => 'value3',
'cib_4_34b2e5386658' => 'value4',
'cib_5_34b2e5386658' => 'value5',
…

masteryoda
- 272
- 2
- 20
1
vote
1 answer
C - Longest subarray of specified elements from given array
I need help with the following problem:
Given an array arr of structs
typedef struct
{
char name[20];
float amount,price;
}product;
Print the longest subarray of elements from array arr such that the arr element has greater or equal price…

ufo
- 93
- 1
- 11
1
vote
3 answers
select subarrays delimited by zeros in python
Given a list like:
A = [18, 7, 0, 0, 0, 9, 12, 0, 0, 11, 2, 3, 3, 0, 0, 7, 8]
is there a simple way to create subarrays, with those elements that are separated by zeros (or at least by NaNs)? I mean, like:
A1 = [18, 7]
A2 = [9, 12]
A3 = [11, 2, 3,…

urgeo
- 645
- 1
- 9
- 19
1
vote
2 answers
d3.js: extract array in object within array
I would like to obtain array in object within array.
Original structure:
Array [ object, ..., object ]
var dateC = [
{
key: "2016-01-01",
values: **[
{city:"", country:""...},
{...}
]**},
{
key:…

shawnngtq
- 687
- 1
- 7
- 24
1
vote
2 answers
R - How to get matrix from multidimensional array
Suppose I've got 5D array arr.
To get 2d matrix with fixed 3rd, 4th and 5th indices I do something like: matr = arr[,,3,2,3]. Suppose I've got list of indices idx = c(3,2,3). Is there any way to get same result using idx? Something like matr =…

am-real
- 54
- 6
1
vote
1 answer
Navigating a 2D array mapped to 1D with boundary conditions
I'm attempting to implement the game of life with a focus on efficiency, as well functionality for pattern matching. Where patterns are blinkers, gliders, crosses, etc.
I've got a 1D array for the world, as well as a width and height. In order to…

Jack
- 804
- 7
- 18