Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My obsession with set theory continues. I believe that most programming languages have neither sets as possible structures nor set theory operations such as Union and Intersection. If I am wrong then this question becomes trivial and I learn something new.

It would be fun to ask for the code golf construction of a full set structure where a set could contain any possible structures within the language and operators such as is a member of, is a subset of, union and intersection etc. However that is much too broad. So I will simplify the puzzle.


An Alphaset has only lower case letters as elements.

The structure for an Alphaset can be defined as you wish and the method used should be explained. It could be just a list of letters read from a file.

As an example I will use an array as a structure for an Alphaset.

An Alphaset can contain a letter once only and order does not matter.

['a','b','c'], ['b','c','a'] and ['c','a','b'] are equivalent Alphasets.

The operation Union forms an Alphaset from two Alphasets.

['a','b','c'] Union ['d','a', 'g', 'b'] is ['a', 'b', 'c', 'd', 'g'] or an equivalent.

A Big Alphaset has the same structure as an Alphaset but can contain repeated letters, eg ['a', 'b', 'a', 'c', 'b'] is a Big Alphaset.

If A and B are any combination of Alphasets and Big Alphasets then A Union B must return an Alphaset.

This gives a way of producing an Alphaset from a Big Alphaset should it ever be necessary for when A is a Big Alphaset the A Union A will be an Alphaset.



A an Alphaset or Big Alphaset

B an Alphaset or Big Alphaset


An Alphaset that is the Union of A and B.

Test Data

Testing should demonstrate that order of elements within the Alphaset does not matter.

ie the following should all produce equivalent results.

['a','b','c'] Union ['a', 'c', 'd']

['b','c','a'] Union ['c', 'd', 'a']

['c','b','a'] Union ['d', 'a', 'c']

Shortest code wins

share|improve this question

closed as off-topic by Doorknob, algorithmshark, Glenn Randers-Pehrson, Justin, Dennis Apr 12 '14 at 5:30

  • This question does not appear to be about programming puzzles or code golf within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

This is actually the built-in Ruby behavior for the | operator on arrays. – histocrat Apr 11 '14 at 18:41
.NET also has sets built-in. (HashSet<T>) – Veronica Deane Apr 11 '14 at 19:42
This question appears to be off-topic because it is extremely trivial. – Doorknob Apr 11 '14 at 23:40

Python2 - 21 (or 15) (or 1)

Sets should be in the format set([element1, element2, element3...])

print input()|input()

If running from the interactive prompt is allowed, remove the print.

Note: If the Mathematica answer qualifies as being 1 character, then in the Python interactive prompt this should also qualify as 1 character:


Sample input:


Sample output:

set(['a', 'c', 'b', 'd', 'k', 'z'])
share|improve this answer
In Python 3, that would be print(eval(input()+"|"+input())). – golfer9338 Apr 11 '14 at 19:25
@golfer9338 You're right, I should probably specify Python2. – ace Apr 11 '14 at 19:31

Mathematica, 1 Chars

It's a built-in feature:

Mathematica graphics

share|improve this answer

APL - 1 char

is the union operator, so 1 2 3∪3 4 5 returns 1 2 3 4 5. In APL, there are no brackets around lists.

Other diadic (two-argument) set operators in APL are (intersection) and ~ (Without):

1 2 3∩3 4 5 = 3, 1 2 3 4 5~1 3 = 2 4 5

Many languages have libraries built especially to deal with sets:

  • Haskell - Data.Set
  • Java - java.util.Set (an interface implemented by 8+ builtin classes)
  • c++ - std::set
  • Ecmascript 6 will have a Set object

Almost every language has some sort of way to handle sets. Usually googling "somelanguage set" will point you towards the documentation for that language's set

share|improve this answer

Bash + coreutils, 30

Not sure how strictly the input and output formats need to be followed, but this does a union correctly:

echo `tr \  "
"<<<$@|sort -u`

In action:

$ ./ "a b c" "a c d"
a b c d
$ ./ "b c a" "c d a"
a b c d
$ ./ "c b a" "d a c"
a b c d
share|improve this answer

Golfscript, 3 bytes



$ echo '["a" "c" "d" "b" "c"] ["f" "b" "e"]'| golfscript
["a" "c" "d" "b" "f" "e"]

How it works

~ # Interpret the input string.
| # Setwise OR (union)
p # print
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.