-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayCompare.js
188 lines (167 loc) · 6.2 KB
/
arrayCompare.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Compare values of two arrays
function compareArrays1 (array1, array2) {
// Filter input to arrays only
if (!(Array.isArray(array1) && Array.isArray(array2))) {
return false
}
// Match all keys in both arrays
for (const index of array1.keys()) {
if (array1[index] !== array2[index]) {
return false
}
}
for (const index of array2.keys()) {
if (array1[index] !== array2[index]) {
return false
}
}
// If no mismatch, arrays are equal
return true
}
// Compare values of N arrays
function compareArrays2 (...arrays) {
if (arrays.length < 2) {
return false
}
for (const [arrayIndex, array] of arrays.entries()) {
// Filter input to arrays only
if (!(Array.isArray(array))) {
return false
}
// Match all keys against last array
if (arrayIndex > 0) {
const lastArray = arrays[arrayIndex - 1]
for (const index of array.keys()) {
if (array[index] !== lastArray[index]) {
return false
}
}
for (const index of lastArray.keys()) {
if (array[index] !== lastArray[index]) {
return false
}
}
}
}
// If no mismatch, arrays are equal
return true
}
// Compare values of two arrays
// Order does not matter
// Duplicates do not matter
function compareSets1 (array1, array2) {
// Filter input to arrays only
if (!(Array.isArray(array1) && Array.isArray(array2))) {
return false
}
// Transform arrays to sets
const arraySet1 = new Set(array1)
const arraySet2 = new Set(array2)
// Match all values in both sets
for (const value of arraySet1) {
if (!arraySet2.has(value)) {
return false
}
}
for (const value of arraySet2) {
if (!arraySet1.has(value)) {
return false
}
}
// If no mismatch, arrays are equal
return true
}
// Compare values of N arrays
// Order does not matter
// Duplicates do not matter
function compareSets2 (...arrays) {
if (arrays.length < 2) {
return false
}
const setArray = []
for (const [arrayIndex, array] of arrays.entries()) {
// Filter input to arrays only
if (!(Array.isArray(array))) {
return false
}
// Transform array to set
const arraySet = new Set(array)
setArray[arrayIndex] = arraySet
// Match all values against last set
if (arrayIndex > 0) {
const lastSet = setArray[arrayIndex - 1]
for (const value of arraySet) {
if (!lastSet.has(value)) {
return false
}
}
for (const value of lastSet) {
if (!arraySet.has(value)) {
return false
}
}
}
}
// If no mismatch, arrays are equal
return true
}
console.assert(compareArrays1([1, 2, 3], [1, 2, 3]) === true, 1)
console.assert(compareArrays1([1, 2, 3], [1, 2]) === false, 2)
console.assert(compareArrays1([1, 2], [1, 2, 3]) === false, 3)
console.assert(compareArrays1([1, 2, 4, 4], [1, 2, 3, 4]) === false, 4)
console.assert(compareArrays1([], []) === true, 5)
console.assert(compareArrays1(1, 1) === false, 6)
console.assert(compareArrays1([]) === false, 7)
console.assert(compareArrays1([], true) === false, 8)
console.assert(compareArrays1([], undefined) === false, 9)
console.assert(compareArrays1(null, []) === false, 10)
console.assert(compareArrays1([], 'string') === false, 11)
console.assert(compareArrays1([], {}) === false, 12)
console.assert(compareArrays1([1, 10, 2, 21], [1, 2, 10, 21]) === false, 13)
console.assert(compareArrays1([1, 10, 2, 21], [1, 2, 2, 10, 21]) === false, 14)
console.assert(compareArrays2([1, 2, 3], [1, 2, 3]) === true, 1)
console.assert(compareArrays2([1, 2, 3], [1, 2]) === false, 2)
console.assert(compareArrays2([1, 2], [1, 2, 3]) === false, 3)
console.assert(compareArrays2([1, 2, 4, 4], [1, 2, 3, 4]) === false, 4)
console.assert(compareArrays2([], []) === true, 5)
console.assert(compareArrays2(1, 1) === false, 6)
console.assert(compareArrays2([]) === false, 7)
console.assert(compareArrays2([], true) === false, 8)
console.assert(compareArrays2([], undefined) === false, 9)
console.assert(compareArrays2(null, []) === false, 10)
console.assert(compareArrays2([], 'string') === false, 11)
console.assert(compareArrays2([], {}) === false, 12)
console.assert(compareArrays2([1, 10, 2, 21], [1, 2, 10, 21]) === false, 13)
console.assert(compareArrays2([1, 10, 2, 21], [1, 2, 2, 10, 21]) === false, 14)
console.assert(compareArrays2([1, 2, 3], [1, 2, 3], [1, 2, 3]) === true, 15)
console.assert(compareArrays2([1, 2, 3], [1, 2, 3], [1, 2, 4]) === false, 16)
console.assert(compareSets1([1, 2, 3], [1, 2, 3]) === true, 1)
console.assert(compareSets1([1, 2, 3], [1, 2]) === false, 2)
console.assert(compareSets1([1, 2], [1, 2, 3]) === false, 3)
console.assert(compareSets1([1, 2, 4, 4], [1, 2, 3, 4]) === false, 4)
console.assert(compareSets1([], []) === true, 5)
console.assert(compareSets1(1, 1) === false, 6)
console.assert(compareSets1([]) === false, 7)
console.assert(compareSets1([], true) === false, 8)
console.assert(compareSets1([], undefined) === false, 9)
console.assert(compareSets1(null, []) === false, 10)
console.assert(compareSets1([], 'string') === false, 11)
console.assert(compareSets1([], {}) === false, 12)
console.assert(compareSets1([1, 10, 2, 21], [1, 2, 10, 21]) === true, 13)
console.assert(compareSets1([1, 10, 2, 21], [1, 2, 2, 10, 21]) === true, 14)
console.assert(compareSets2([1, 2, 3], [1, 2, 3]) === true, 1)
console.assert(compareSets2([1, 2, 3], [1, 2]) === false, 2)
console.assert(compareSets2([1, 2], [1, 2, 3]) === false, 3)
console.assert(compareSets2([1, 2, 4, 4], [1, 2, 3, 4]) === false, 4)
console.assert(compareSets2([], []) === true, 5)
console.assert(compareSets2(1, 1) === false, 6)
console.assert(compareSets2([]) === false, 7)
console.assert(compareSets2([], true) === false, 8)
console.assert(compareSets2([], undefined) === false, 9)
console.assert(compareSets2(null, []) === false, 10)
console.assert(compareSets2([], 'string') === false, 11)
console.assert(compareSets2([], {}) === false, 12)
console.assert(compareSets2([1, 10, 2, 21], [1, 2, 10, 21]) === true, 13)
console.assert(compareSets2([1, 10, 2, 21], [1, 2, 2, 10, 21]) === true, 14)
console.assert(compareSets2([1, 2, 3], [1, 2, 3], [1, 2, 3]) === true, 15)
console.assert(compareSets2([1, 2, 3], [1, 2, 3], [1, 2, 4]) === false, 16)