-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlof_condense.js
65 lines (53 loc) · 2.06 KB
/
lof_condense.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
function LoFCondense(nestedArray) {
// Helper function to find all identical subarrays at all levels
function findIdenticalPairs(arr) {
let pairs = [];
let seen = new Map();
function traverse(currentArr, path) {
if (!Array.isArray(currentArr)) return;
let key = JSON.stringify(currentArr);
if (seen.has(key)) {
pairs.push([seen.get(key), path]);
} else {
seen.set(key, path);
}
for (let i = 0; i < currentArr.length; i++) {
traverse(currentArr[i], path.concat(i));
}
}
traverse(arr, []);
return pairs;
}
// Helper function to remove a subarray at a given path
function removeAtPath(arr, path) {
if (path.length === 1) {
arr.splice(path[0], 1);
} else {
removeAtPath(arr[path[0]], path.slice(1));
}
}
// Find all identical pairs of subarrays
let identicalPairs = findIdenticalPairs(nestedArray);
if (identicalPairs.length === 0) {
return nestedArray;
}
// Select a random pair to condense
let randomPair = identicalPairs[Math.floor(Math.random() * identicalPairs.length)];
let [firstPath, secondPath] = randomPair;
// Remove the second occurrence of the identical subarray
removeAtPath(nestedArray, secondPath);
// Ensure the first occurrence remains
return nestedArray;
}
{
// Test cases
let testCase1 = [[], []];
let testCase1b = [[[], []]];
let testCase2 = [[[[], []]], [[[], []]]];
let testCase3 = [[[[], []]], [[[], []]]];
console.log(JSON.stringify(LoFCondense(testCase1))); // Output: [[]]
console.log(JSON.stringify(LoFCondense(testCase1b))); // Output: [[]]
console.log(JSON.stringify(LoFCondense(testCase2))); // Possible outputs: [[[[]]], [[[], []]]], [[[[], []]]]
console.log(JSON.stringify(LoFCondense(testCase3))); // Possible outputs: [[[[]]], [[[], []]]], [[[[], []]]]
console.log(JSON.stringify(LoFCondense(testCase3))); // Possible outputs: [[[[]]], [[[], []]]], [[[[], []]]]
}