The challenge is to create a program/function that prints the intersection of its own source code and a given string input. This is code golf and to be more precise:

  • Let I be the input set
    • {"a","b","c"}
  • Let S be the source code set
    • {"b","f"}
  • Then the intersection is what they share
    • I ∩ S = {"b"}


Input is flexible. It should be able to handle the character encoding used for the source code.


Output is flexible. It should be the set of characters that the input and source code share. Also, sets are unordered collections of distinct objects. In summary:

  • Output is flexible:
    • Could be any data structure (string or otherwise)
    • Could unordered
    • Could have a trailing \n
    • Should be distinct


Similar to challenges, the program/function may not read its own source code and 0-byte solutions are not allowed.


  • #1
functor x(I){ return I ∩ self; }

Inputs                                Outputs
------                                -------
enter preformatted text here      ->  {"e","n","t","r","f","o","x"}

["Albrt"," Einstin"]              ->  {"l","r","t","n","s"}
  • #2

Inputs                                Outputs
------                                -------
"Security at the expense of       ->  "
usability comes at the expense 
of security."

(0____0)                          ->  (_)
  • #3

Inputs                                Outputs
------                                -------
Far out in the uncharted backwaters ->"a"    
of the unfashionable end of the 
Western Spiral arm of the Galaxy lies 
a small unregarded yellow sun. 
Orbiting this at a distance of roughly 
ninety-eight million miles is an 
utterly insignificant little blue-green 
planet whose ape-descended life forms 
are so amazingly primitive that they 
still think digital watches are a pretty 
neat idea.

(ノಠ益ಠ)ノ彡┻━┻                      ->"ಠ益"

Test Cases

Albert Einstein

\__( O__O)_/





┬──┬ ノ( ゜-゜ノ)

Far out in the uncharted backwaters of the unfashionable end of the Western Spiral arm of the Galaxy lies a small unregarded yellow sun. Orbiting this at a distance of roughly ninety-eight million miles is an utterly insignificant little blue-green planet whose ape-descended life forms are so amazingly primitive that they still think digital watches are a pretty neat idea.


  • [16-08-10]: sets are unordered collections of distinct objects
  • [16-08-10]: trailing newline is acceptable
  • 2
    \$\begingroup\$ May the output contain duplicate characters? \$\endgroup\$ – Digital Trauma Aug 10 '16 at 18:44
  • 1
    \$\begingroup\$ @DigitalTrauma From examples #1, #2 and #3 it appears not \$\endgroup\$ – Luis Mendo Aug 10 '16 at 18:50
  • \$\begingroup\$ @DigitalTrauma Sorry for the ambiguity, sets (in the mathematical sense) ignore order and have no repetition. \$\endgroup\$ – NonlinearFruit Aug 10 '16 at 19:05
  • 23
    \$\begingroup\$ Congratulations for coming up with a generalised quine where the best solutions are not based on the language's standard quine. :) \$\endgroup\$ – Martin Ender Aug 10 '16 at 20:27
  • 1
    \$\begingroup\$ If a set should not have repetition, shouldn't the input sets also not contain repetition? Or is the input actually not a set? \$\endgroup\$ – user81655 Aug 11 '16 at 15:37

45 Answers 45


Jelly, 10 6 bytes


Try it online!

How it works

“Ṿf”Ṿf  Main link. Argument: s (string)

“Ṿf”    Set the return value to 'Ṿf'.
    Ṿ   Uneval; yield '“Ṿf”'.
     f  Filter; remove the characters from '“Ṿf”' that do not appear in s.

Python 3, 44 bytes

Thanks Karl for saving me one byte :-) Thanks Dada for saving me two bytes!

I think this works, but it's my first quine challenge so I'm not 100% sure. :\


Lambda version with 43 bytes: lambda a:set(" lambdaset()&'"+':+"')&set(a)

  • 8
    \$\begingroup\$ 'eroticpuns\()&\'' is shorter than adding the strings. (escaped the ' but then you need a additional `\`) Why is the dot there? \$\endgroup\$ – KarlKastor Aug 10 '16 at 19:26
  • \$\begingroup\$ woops, the . was a relic from less golfed code. Using backslash doesn't work because then the output from \` is \\`, and duplicating the input character isn't allowed, I think. \$\endgroup\$ – Jeremy Aug 10 '16 at 19:32
  • 1
    \$\begingroup\$ Your lambda is missing the :. \$\endgroup\$ – Dennis Aug 10 '16 at 19:45
  • \$\begingroup\$ Thanks @Dennis. My eyes start clouding over after trying to find all the characters in the program ;) \$\endgroup\$ – Jeremy Aug 10 '16 at 19:48
  • 1
    \$\begingroup\$ @Dada: If you'd prefer, it could be inspectour, nicestupor, poeticurns, nopictures, recountspi, or inputscore. Or for the new one you might use prunesit (an accurate description of what code golfers do!), ipunster, or nursepit among others. \$\endgroup\$ – Deusovi Aug 11 '16 at 6:53

Dyalog APL, 8 bytes


is returns those characters from the left argument that are present in the right argument (if the left argument has no duplicates – as in this case – then the result also has no duplicates

is the argument

Then the string just has those two plus the quote character (doubled, as it is in a string).

TryAPL online!


GolfScript, 6 bytes


Try it online!

How it works

        # (implicit) Push the input on the stack.
"`&"    # Push the string '`&' on the stack.
   `    # Inspect; turn the string into '"`&"'.
     &  # Perform set intersection.
  • \$\begingroup\$ So that's how you can get quotes into the string without having to add backslash. \$\endgroup\$ – Pseudo Nym Dec 18 '19 at 13:00

Python 2, 56 46 39 Bytes

-1 Byte thanks to @Jeremy

lambda a:set(':&()smelt\ bad\'')&set(a)

anonymous lambda function, takes a string, returns a set

old version:

lambda x,w=set('newmatrixbuspdl_:-)(=,\ \''):w-(w-set(x))
  • \$\begingroup\$ I like this, but it returns two backslashes on `\\` instead of just one. \$\endgroup\$ – Jeremy Aug 10 '16 at 19:35
  • \$\begingroup\$ Also, i think you can save a byte by changing the name of the lambda to a \$\endgroup\$ – Jeremy Aug 10 '16 at 19:37
  • 1
    \$\begingroup\$ @Jeremy Thanks for tip, '\\' is just Python's way of representing a backslash in string form, because a single one would escape the end quote, so you have to escape the backslash with a backslash to make it work. Type print '\\' and you'll see that it is just the representation for one backslash. \$\endgroup\$ – KarlKastor Aug 10 '16 at 20:12
  • \$\begingroup\$ You can get to 36 with lambda a:{*''' lambda&':{}*'''}&{*a}. \$\endgroup\$ – Morgan Thrapp Aug 11 '16 at 16:08
  • 1
    \$\begingroup\$ @MorganThrapp 35 lambda a:{*' lambda&\\\':{}*'}&{*a} \$\endgroup\$ – seequ Aug 11 '16 at 20:44

Perl 6, 56, 55 bytes

"French" / Unicode version (55 bytes)

say perl q.say perlq∩$*IN\\\.comb:..comb∩$*IN.comb:

"Texas" / ASCII versions (56 bytes)

say (q.sayq(&) $*IN\\\.combperl..comb (&)$*IN.comb).perl
say perl q.sayq(&) $*IN\\\.comb:perl..comb (&)$*IN.comb:


my \Source = 'my \\Source = \'say ( $*IN.comb.Set ∩ Source.comb.Set ).perl\'';
say ( $*IN.comb.Set ∩ Source.comb.Set ).perl


$ echo -n 'say perl q.say perlq∩$*IN\\\.comb:..comb∩$*IN.comb:' > test-unicode.p6

$ echo -n 'say (q.sayq(&) $*IN\\\.combperl..comb (&)$*IN.comb).perl' > test-ascii.p6

$ perl6 test-ascii.p6 <<< 'abcdefghijklmnopqrstuvwxyz'

$ perl6 test-unicode.p6 < test-unicode.p6
set("\\","I","p"," ","a","c","l","r","q","b","∩","*","s","m","e",".","y",":","o","N","\$")

$ perl6 test-ascii.p6 < test-ascii.p6
set("p","\\","I"," ","a","l","r","c","q","b",")","*","s","e","m","\&",".","(","y","o","N","\$")

$ perl6 test-ascii.p6 < test-unicode.p6
set("p","\\","I"," ","a","l","r","c","q","b","*","s","e","m",".","y","o","N","\$")

$ perl6 test-unicode.p6 <<< 'Albert Einstein'
set(" ","l","r","b","s","e")

$ perl6 test-unicode.p6 <<< '\__( O__O)_/'
set("\\"," ")

$ perl6 test-ascii.p6 <<< '!@#$%^&*()_+{}|:"<>?'

$ perl6 test-unicode.p6 <<< "1234567890-=[]\\;',./"

$ perl6 test-unicode.p6 <<< '(ノಠ益ಠ)ノ彡┻━┻'


$ perl6 test-unicode.p6 <<< '┬──┬ ノ( ゜-゜ノ)'
set(" ")

$ perl6 test-ascii.p6 <<< 'Far out in the uncharted backwaters of the unfashionable end of the Western Spiral arm of the Galaxy lies a small unregarded yellow sun. Orbiting this at a distance of roughly ninety-eight million miles is an utterly insignificant little blue-green planet whose ape-descended life forms are so amazingly primitive that they still think digital watches are a pretty neat idea.'
set("p"," ","a","l","r","c","b","s","e","m",".","y","o")
  • 2
    \$\begingroup\$ Doesn't $*PROGRAM access the source code of the program, and thus violate the rules? \$\endgroup\$ – celtschk Aug 12 '16 at 8:34
  • \$\begingroup\$ @celtschk I should have re-read the question before posting, fixed. ( Technically the compiler could see that $*PROGRAM is read from and store the entire source as a string in the compiled program, which would have put it into a grey area ) \$\endgroup\$ – Brad Gilbert b2gills Aug 15 '16 at 0:29

MATL, 8 bytes


Try it online!

Input is a string enclosed in single quotes. If the string contains a single-quote symbol, it should be duplicated to escape it.


'X&'''   % Push string with the three characters used by the program. The single-quote 
         % symbol needs to be escaped by duplicating it
X&       % Take input implicitly. Set intersection. Display implicitly

Actually, 6 bytes


Try it online!


`∩è`    push the function `∩è` (which contains every character in the source code except '`')
    è   repr (same as Python repr - leaves "`∩è`", which contains every character in the source code)
      ∩ set intersection with input

05AB1E, 11 bytes



Uses the CP-1252 encoding. Try it online!.


Haskell (30 bytes)

This is such a boring solution... But I couldn't do better. :(


Brachylog, 23 bytes


Try it online!


:{                 }f      Find all chars that verify the predicate below
                     d.    Remove duplicates and output

  e.                       Take a char from the input ; this is our output…
    ~e":{}e~\"fd\."        … if that char is in the string :{}e~"fd. (the first \ is here
                               to escape the ")
  • 1
    \$\begingroup\$ Can you have a look at our chatroom? \$\endgroup\$ – Leaky Nun Aug 11 '16 at 9:56
  • 1
    \$\begingroup\$ +1 for moustache man :{ and surprised moustache man :{} \$\endgroup\$ – Destructible Lemon Aug 17 '16 at 4:29

C, 142 bytes


Try it on ideone.

  • 2
    \$\begingroup\$ An ungolfed version and/or explanation would be great! \$\endgroup\$ – YSC Aug 11 '16 at 11:58
  • \$\begingroup\$ You could have used sizeof a instead of strlen(a) for one byte saved, but even better is to give the array a known size: a[99]="...", and replace strlen(a) by 99 to shave off 5 bytes. \$\endgroup\$ – G. Sliepen Sep 8 '16 at 11:58
  • \$\begingroup\$ Another 3 or 4 bytes can be saved by replacing (i=getchar())>=0 with read(0,&i,1). This works on little-endian machines. i is initialized to 1 if you don't run the program with any arguments. If you want it to work on big-endian machines as well, remove i from the argument list of main() and declare it inside the body as a char (but then you save only 3 bytes). read() conveniently returns 0 on EOF. \$\endgroup\$ – G. Sliepen Sep 8 '16 at 12:03

CJam, 8 bytes


Try it here.


"`q&"    e# Push that string to the stack
     `   e# Stringify, pops the string and pushes "\"`r&\"" to the stack
      q  e# Pushes the input to the stack
       & e# Union, pops two elements and pushes a list of every element that is contained in both.

Pyth, 9 8 bytes

1 byte thanks to Sp3000


Try it online!

@Q"\Q@\""   implicit string ending

@Q           intersect the input with
  "\Q@\""   the string containing '\', 'Q', '@', '"'.
  • \$\begingroup\$ @z"z\"@ is 1 byte shorter. \$\endgroup\$ – drobilc Aug 13 '16 at 11:23
  • \$\begingroup\$ @drobilc That would miss out the \. \$\endgroup\$ – Leaky Nun Aug 13 '16 at 12:26
  • \$\begingroup\$ oh, yeah, totally forgot about that. \$\endgroup\$ – drobilc Aug 13 '16 at 12:32

Retina, 21 20 bytes

Removes characters not in the source code, then removes duplicate characters.



Try it online

  • \$\begingroup\$ Your source code contains a linefeed (your output doesn't). \$\endgroup\$ – Martin Ender Aug 11 '16 at 17:58
  • \$\begingroup\$ I had basically the same solution earlier but forgot to post it. You can save a few bytes with the range [-a (and then include an underscore and a hyphen and drop the backtick on the second line). But for future reference, ] wouldn't need escaping if you put it as the first character. Oh and for niceness, you can swap the two stages to avoid the trailing linefeed. \$\endgroup\$ – Martin Ender Aug 11 '16 at 19:41
  • \$\begingroup\$ @MartinEnder This still doesn't deduplicate line feeds, is that a problem? \$\endgroup\$ – mbomb007 Aug 12 '16 at 13:39
  • \$\begingroup\$ Oh you're right, I didn't notice that. You'll have to add s to the options of D and the character class then. \$\endgroup\$ – Martin Ender Aug 12 '16 at 13:41

Mathematica, 35 bytes


Anonymous function. Ignore any generated messages. Takes a list of characters as input and returns a list of characters as output. The Unicode character is U+22C2 for \[Intersection].


C#, 36 bytes


Intended cast is Func<string, IEnumerable<char>> (string input, IEnumerable<char> output).


Vim, 78 68 78 79 61 keystrokes

Completely changed my approach:


How it works:

First, it makes a line with all the program characters, then, it finds the first instance of each of the program characters, which is either in the input, if the input and output intersect, or the output if they don't, deletes it, moves to the last character of the file (so it wraps around) and does that for each unique character in source, except d, where instead of moving to the end of the file, it finishes up by deleting the input

  • \$\begingroup\$ Backslash is in your code but doesn´t seem to be in your string. \$\endgroup\$ – Titus Dec 21 '16 at 16:54
  • \$\begingroup\$ Isn't it the third? \$\endgroup\$ – Destructible Lemon Dec 21 '16 at 20:11

Bash, 45 50 41 39 37 34 29 bytes

-9 bytes thanks to Geoff Reedy
-4 bytes thanks to Dennis
-5 bytes thanks to Nahuel Fouilleul

grep -o '[] [|\'\'grepouniq-]

Try it online!

  • \$\begingroup\$ Don't you only need one grep command? \$\endgroup\$ – Geoff Reedy Aug 11 '16 at 23:21
  • \$\begingroup\$ @GeoffReedy The first grep command splits the input into one character per line. \$\endgroup\$ – Dennis Aug 12 '16 at 3:43
  • \$\begingroup\$ Right but couldn't the -o be put on to the second grep \$\endgroup\$ – Geoff Reedy Aug 12 '16 at 12:52
  • \$\begingroup\$ You're right, and that saves having to check for the '.'. Thanks! \$\endgroup\$ – Riley Aug 12 '16 at 14:11
  • 1
    \$\begingroup\$ @Titus After BASH does it's thing, grep gets -o and [] [|\'grepouniq-]. So it is looking for anything of these: [ ] {space} [ | {slash} ' g r e p o u n i q - ]. \$\endgroup\$ – Riley Dec 21 '16 at 17:09

PowerShell v4+, 122 104 bytes


Ugh. Quines or quine-like code in PowerShell sucks, because the string replacement formatting is so clunky.

The string ace...{2} in the middle is every character that's present in the rest of the code. The {0}{1}{2} is used in conjunction with the -format operator to pull the '{} characters into the string.

That's combined as a char-array with the input $args, then fed into the pipeline. The first stop is Group-Object which (essentially) creates a hashtable of the input objects and how many times they occur in the input. That's piped to |?{...} the Where-Object to only select those items that have a .count greater than 1. We encapsulate that in parens, and pluck out the .Name portion of the hashtable (which is where the v4+ requirement comes into play, otherwise we'd need an additional |Select Name stage to the pipeline).

Those elements are left on the pipeline (as an array), and printing is implicit.


Python 2, 44 bytes

x='c=set;print c(`x`)&c(raw_input())';exec x

Just for fun, here's a quine-like full program submission. Outputs the string representation of a Python 2 set.


JavaScript (ES6), 59 57 bytes

<input placeholder=Input oninput=o.value=f(this.value).join``><input placeholder=Output id=o>

Returns an array of characters present in both the original string/character array and the source code. Edit: Saved 2 bytes thanks to @user81655.

  • \$\begingroup\$ f=s=>[...new Set(f+'')]... could save bytes. \$\endgroup\$ – user81655 Aug 11 '16 at 15:17
  • \$\begingroup\$ Or even shorter: f=s=>[...s].filter(c=>(new Set(f+'')).has(c)) \$\endgroup\$ – user81655 Aug 11 '16 at 15:27
  • \$\begingroup\$ @user81655 At least in Firefox, f+'' works by reading f's source code. (In certain cases you could get Firefox to crash by changing the source file and then attempting to stringify a function loaded from it.) \$\endgroup\$ – Neil Aug 11 '16 at 15:27
  • \$\begingroup\$ @user81655 Your second example fails when s has repeated elements, and indexOf is shorter than new Set anyway. \$\endgroup\$ – Neil Aug 11 '16 at 15:30
  • \$\begingroup\$ In that case, you could try saving chars (like changing the c parameter to a character that is already in the list). \$\endgroup\$ – user81655 Aug 11 '16 at 15:30

Matlab, 37 bytes

Quite simple:

Uses the builtin intersect to find the intersection. The source code is hard coded. Input must be given inside quotation marks ''

  • \$\begingroup\$ You should've used an anonymous function...beat you by 5 bytes \$\endgroup\$ – Sanchises Dec 21 '16 at 15:46
  • \$\begingroup\$ Hehe, I guess that wasn't my best golf... \$\endgroup\$ – Stewie Griffin Dec 21 '16 at 17:07

JavaScript (Chrome 58 on OS X 10), 12654 12426 11992 Bytes




The original code:

var t=prompt();"!+()[]".split("").forEach(function(f){if(t.includes(f))alert(f)})

This was then converted into a programming style called jsfk which only uses these six characters:


using an online compiler.

  • \$\begingroup\$ If jsfk is the language, you should use that in the header instead of Javascript. \$\endgroup\$ – NonlinearFruit Sep 23 '17 at 14:00
  • 1
    \$\begingroup\$ @NonlinearFruit jsfk is a programming style. It is valid javascript \$\endgroup\$ – Tornado547 Nov 28 '17 at 16:01

R, 129 bytes


If I ungolf it, it needs to have weird things changed like a newline in the string for b. Anyhow, its super simple -- builds a vector with all characters in the function in it. Then it pulls the input into a vector, and checks membership.

  • 1
    \$\begingroup\$ you haven't visited the site in almost a year, but f=function(s)cat(instersect(strsplit(s,"")[[1]],strsplit("f=unctio(s)aerpl,\\\"[1]","")[[1]]),sep="") is 101 bytes, and I think the I/O format can be simpler, without cat... \$\endgroup\$ – Giuseppe Aug 8 '17 at 19:26

Ruby, 34 + n flag = 35 bytes

Doesn't exactly work with multi-lined input, since -n causes the program to process STDIN line-by-line. There aren't newlines in this code, but trying to input something like that will output multiple arrays instead of one. If that is not good according to spec, please inform me and I will fix.

p $_.chars&"\\\"p $_.chars&".chars

ListSharp, 222 bytes

STRG S=READ[<here>+"\\S.txt"]
ROWS R=ROWSPLIT "STRG =EAD[<her>+\".tx]OWPLIBYCFMHVNc#isn()oay\r\n" BY [""]

ridiculous but im entertained


sed, 47 characters


I'm a little disappointed at how long this came out to be, especially the bit to remove repeated characters.

  • \$\begingroup\$ Which version of sed is this? GNU sed says sed: -e expression #1, char 47: unterminated `s' command. \$\endgroup\$ – Dennis Aug 12 '16 at 3:03
  • \$\begingroup\$ 43 bytes including 1 for -r: sed -r ':;ss[^][str().*\12;:^]ss;ss(.)(.*\1)s\2s;t' i wrote it before noticing yours and it turned out to be very similar \$\endgroup\$ – izabera Aug 12 '16 at 7:23
  • \$\begingroup\$ @Dennis fixed; turned out having a : after the [ caused it to try and parse a character class \$\endgroup\$ – Geoff Reedy Aug 12 '16 at 13:04
  • \$\begingroup\$ @izabera nice, I'm quite surprised that the : command doesn't actually need a label and that it changes the meaning of t without a label \$\endgroup\$ – Geoff Reedy Aug 12 '16 at 13:07
  • \$\begingroup\$ yeah it's a gnuism \$\endgroup\$ – izabera Aug 12 '16 at 13:25

Java 8 lambda, 152 142 140 characters

Quite short:


Or ungolfed over here:

public class Q89400 {

    static Set<Character> inAndQuine(String in) {
        return in.chars()


Of course the ungolfed solution is wrong, as it doesn't match the curly brackets and some more characters, it's just their for the sake of completeness.

The function takes input as a String and returns a java.util.Set<Character> containing the characters which are present in both input and source.


It turned out that the solution wasn't working. I thought String#contains tests for a regex match but it is just a literal matching. I added some escaping to quote the characters like . but this wasn't necessary but ruined everything instead. Now without this escaping we save some characters and now it actually works :)

Thanks to @NonlinearFruit for reminding me of using one-character variables.

  • \$\begingroup\$ Rename in to be a single letter like a \$\endgroup\$ – NonlinearFruit Aug 12 '16 at 12:16
  • 3
    \$\begingroup\$ @NonlinearFruit you're right :O how could I forget about that?! \$\endgroup\$ – Frozn Aug 12 '16 at 13:13

SQF, 71 69 64 bytes

Using the file-as-a-function format:

i="-_h ;""=()sSplitrng"splitString"";i-(i-(_this splitString""))



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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