-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayOfArrayOfStrings.js
41 lines (37 loc) · 1.49 KB
/
arrayOfArrayOfStrings.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
var data = [
['Child1', 'Child2'],
['Child3', 'Child4'],
['Child4'],
['Child2', 'Child1'],
['Child5', 'Child1', 'Child2'],
['Child6', 'Child7']
];
find(data, function(idx, els) { document.write("Array[" + idx + "] - "
+ els.toString().replace(',', ', ')
+ "; "); });
function find(dataArray, callback) {
for (var i = 0; i < dataArray.length; i++) {
var result = findDuplicateOrNonUniqueElements(dataArray, i);
if (result.foundDuplicate) continue; // duplicate found, non-unique parent
if (result.nonUniqueElements.length != 0) callback(i, result.nonUniqueElements); // non-unique children found
}
}
// array of array of strings
function findDuplicateOrNonUniqueElements(dataArray, idx) {
var el = dataArray[idx];
var els = [];
for (var i = 0; i < dataArray.length; i++) {
if (i == idx) continue; // don't compare to itself
if (equals(dataArray[i], el)) return { foundDuplicate : true, nonUniqueElements : [] }; // found duplicated parent
for (var j = 0; j < el.length; j++) {
if (dataArray[i].indexOf(el[j]) != -1 && els.indexOf(el[j]) == -1) { // non-unique children found
els.push(el[j]);
}
}
}
return { foundDuplicate : false, nonUniqueElements : els };
}
// array of strings
function equals(arr1, arr2) {
return (arr1.sort().toString() === arr2.sort().toString());
}