Merge: doc: fixed some typos and other misc. corrections
[nit.git] / examples / rosettacode / symmetric_difference.nit
1 #!/usr/bin/env nit
2 #
3 # This file is part of NIT ( http://www.nitlanguage.org ).
4 # This program is public domain
5
6 # Task: Symmetric difference
7 # SEE: <http://rosettacode.org/wiki/Symmetric_difference>
8
9 module symmetric_difference
10
11 var a = ["John", "Serena", "Bob", "Mary", "Serena"]
12 var b = ["Jim", "Mary", "John", "Jim", "Bob"]
13
14 print "Set A: {a.join(", ")}"
15 print "Set B: {b.join(", ")}"
16
17 # Option 1: union of differences
18 var not_in_a = new ArraySet[String]
19 not_in_a.add_all(b)
20 for x in a do
21 not_in_a.remove_all(x)
22 end
23
24 var not_in_b = new ArraySet[String]
25 not_in_b.add_all(a)
26 for x in b do
27 not_in_b.remove_all(x)
28 end
29
30 var difference = not_in_a.clone
31 difference.add_all(not_in_b)
32
33 print "Symmetric difference: {difference.join(", ")}"
34
35 # Option 2: union minus intersection
36 var union = new ArraySet[String]
37 union.add_all(a)
38 union.add_all(b)
39
40 var b1 = new ArraySet[String]
41 b1.add_all(b)
42
43 var intersect
44 intersect = new ArraySet[String]
45 intersect.add_all(a)
46 intersect = intersect.intersection(b1)
47
48 var difference2 = union.clone
49 for x in intersect do
50 difference2.remove_all(x)
51 end
52
53 print "Symmetric difference: {difference2.join(", ")}"