-
Notifications
You must be signed in to change notification settings - Fork 27
/
#Arrays.gs
69 lines (62 loc) · 1.72 KB
/
#Arrays.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// https://stackoverflow.com/a/1887494/5372400
function test_intersection()
{
Logger.log(arrayIntersection( [1, 2, 3, "a"], [1, "a", 2], ["a", 1] )); // Gives [1, "a"];
Logger.log(arrayIntersection( [1], [1,6,7,8] )); // Gives [1];
}
var arrayContains = Array.prototype.indexOf ?
function(arr, val) {
return arr.indexOf(val) > -1;
} :
function(arr, val) {
var i = arr.length;
while (i--) {
if (arr[i] === val) {
return true;
}
}
return false;
};
function arrayIntersection() {
var val, arrayCount, firstArray, i, j, intersection = [], missing;
var arrays = Array.prototype.slice.call(arguments); // Convert arguments into a real array
// Search for common values
firstArray = arrays.pop();
if (firstArray) {
j = firstArray.length;
arrayCount = arrays.length;
while (j--) {
val = firstArray[j];
missing = false;
// Check val is present in each remaining array
i = arrayCount;
while (!missing && i--) {
if ( !arrayContains(arrays[i], val) ) {
missing = true;
}
}
if (!missing) {
intersection.push(val);
}
}
}
return intersection;
}
function test_getFilled2DArray()
{
var value = 'max';
Logger.log(getFilled2DArray(value, 3, 4));
// [[max, max, max], [max, max, max], [max, max, max], [max, max, max]]
}
function getFilled2DArray(value, w, h)
{
var result = [];
var row = [];
for (var i = 0; i < h; i++)
{
row = [];
for (var ii = 0; ii < w; ii++) { row.push(value); }
result.push(row);
}
return result;
}