Junior — Middle
Can you explain the concept of union in the context of programming or set theory?
sobes.tech AI
Answer from AI
In set theory, the union of two sets A and B is a set containing all elements that belong to either A, B, or both simultaneously. It is denoted as A ∪ B.
In programming, the concept of union is often encountered in the context of collections or data types. For example, the union of two lists is a new list that includes all elements from both original lists, without duplicates (if dealing with sets).
Example in Java using Set:
Set<Integer> setA = new HashSet<>(Arrays.asList(1, 2, 3));
Set<Integer> setB = new HashSet<>(Arrays.asList(3, 4, 5));
Set<Integer> union = new HashSet<>(setA);
union.addAll(setB); // union now contains 1, 2, 3, 4, 5
Thus, union is an operation that combines elements from two sources into one, without repetitions.