-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript1.js
More file actions
42 lines (35 loc) · 1.07 KB
/
Script1.js
File metadata and controls
42 lines (35 loc) · 1.07 KB
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
function sumOfDistinctElements(set1, set2) {
let sum = 0;
// Create arrays to track which elements are distinct
const countMap = {};
// Count occurrences of each element in set1
for (let i = 0; i < set1.length; i++) {
if (countMap[set1[i]] === undefined) {
countMap[set1[i]] = 1;
} else {
countMap[set1[i]]++;
}
}
// Count occurrences of each element in set2
for (let i = 0; i < set2.length; i++) {
if (countMap[set2[i]] === undefined) {
countMap[set2[i]] = 1;
} else {
countMap[set2[i]]++;
}
}
// Sum elements that appear exactly once across both sets
for (let key in countMap) {
if (countMap[key] === 1) {
sum += parseInt(key);
}
}
return sum;
}
// Test with the example
const set1 = [3, 1, 7, 9];
const set2 = [2, 4, 1, 9, 3];
const result = sumOfDistinctElements(set1, set2);
console.log(`Set 1: [${set1}]`);
console.log(`Set 2: [${set2}]`);
console.log(`Sum of distinct elements: ${result}`);