19
\$\begingroup\$

Based on this question from Code Review

Given a non-empty string of printable ASCII characters, output the second non-repeating character. For example, for input DEFD, output F.

Input

Output

  • The second character that doesn't repeat, when reading left-to-right, again in a suitable format.
  • The output character is case-insensitive.
  • If no such character exists (e.g., all characters repeat), output an empty string.

Rules

  • The algorithm should ignore case. That is, D and d count as the same character.
  • Either a full program or a function are acceptable.
  • The input string will be guaranteed non-empty (i.e., at least one character in length).
  • The input string is ASCII. Any valid character could repeat, not just alphanumeric (this includes spaces).
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.

Examples

Input is on first line, output is on second line.

DEFD
F

FEED
D

This is an example input sentence.
x

...,,,..,,!@
@

ABCDefgHijklMNOPqrsTuVWxyz
B

AAAAAABBBBB


Thisxthis


This this.
.
\$\endgroup\$
4
  • 8
    \$\begingroup\$ If it wasn't case-insensitive, I'd consider doing it in Forth. String operations suck in that language, though. \$\endgroup\$
    – mbomb007
    Commented Jul 1, 2016 at 17:05
  • \$\begingroup\$ What if my language doesn't support lowercase letters? \$\endgroup\$
    – Adám
    Commented Jul 3, 2016 at 10:32
  • \$\begingroup\$ @Adám Does it utilize a different code page? How would it normally input an ASCII string if it doesn't support lowercase letters? \$\endgroup\$ Commented Jul 4, 2016 at 13:37
  • 3
    \$\begingroup\$ The system I had in mind had 7-bit code page; a modified standard code page where uppercase letters occupy the lowercase positions, and the uppercase positions were used for glyphs. This was done on old APL systems so that one could use Shift to access APL glyphs, while unshifted letters were classic coding-style capitals. \$\endgroup\$
    – Adám
    Commented Jul 4, 2016 at 13:46

39 Answers 39

1
2
0
\$\begingroup\$

Jelly, 15 bytes

Œlµċ@Ị¥Ðf¹ḊḢȯ“”

Try it online!

Verify all testcases. (Slightly modified to cater for all testcases)

\$\endgroup\$
0
\$\begingroup\$

Perl, 75 bytes

 my$s=<>;chomp$s;my$c;for my$i(split//,$s){my$m=@{[$s=~/$i/gi]};$m<2and++$c>=2and say$i and last}
\$\endgroup\$
0
\$\begingroup\$

Javascript (using external Library) (107 bytes)

Crushed this using a library I wrote. Not sure if I have to count the declaration of variable "s", which is the string in question.

(s)=>_.From(s).ToLookup(y=>y.toLowerCase(),z=>z).Where(g=>g.Value.Count()==1).Select(x=>x.Key).ElementAt(1)

This will handle an empty string input, an input with only one non-repeating character, and an input with 2+ non-repeating characters

Image 1

\$\endgroup\$
4
  • \$\begingroup\$ Do you have a link to the library in question? Also, this being code golf, you gotta take out whitespace where you can \$\endgroup\$
    – Value Ink
    Commented Jul 7, 2016 at 0:24
  • \$\begingroup\$ Hey, yeah it's github.com/mvegh1/Enumerable . No docs yet. Sorry, I will clean up this answer to reduce as much whitespace \$\endgroup\$ Commented Jul 7, 2016 at 0:25
  • \$\begingroup\$ You should probably mention and link it in the answer body. Also, regarding the bytecount, consensus is to put it into an anonymous lambda (so s=> ...) \$\endgroup\$
    – Value Ink
    Commented Jul 7, 2016 at 0:41
  • \$\begingroup\$ Ok no problem. I didn't want to offend anybody by linking to my code, but I did mention I used my library. I'll update my answer with the lambda, thanks for letting me know \$\endgroup\$ Commented Jul 7, 2016 at 0:45
0
\$\begingroup\$

Clojure, 109 bytes

#(let[s(clojure.string/lower-case %)](or(second(remove(set(map(fn[[k v]](if(> v 1)k))(frequencies s)))s))""))

Ough, I hope there is a more succinct way.

\$\endgroup\$
0
\$\begingroup\$

JavaScript, 57 bytes

v=>(v+'\0'+v).replace(/(.)(?=.*\1.*\1)|\0.*/gi,"")[1]||""

JavaScript doesn't have Regex lookbehind, so I appended a null character and another copy of the string so that I can use lookahead. I then remove the null character and all characters after it as part of the regex replace.

\$\endgroup\$
0
\$\begingroup\$

PowerShell, 55 Bytes

old but good challenge, deserves a PS answer.

([char[]]"$args".ToLower()|group|? Count -eq 1)[1].Name

Throws errors when there are no non-repeating chars, exits with no error or return if there is only one repeating char.

the .ToLower() cost a bit due to the case insensitivity, and using |% ToL*r actually costs bytes since you need to wrap it all in brackets before converting to a char[]

\$\endgroup\$
0
\$\begingroup\$

PHP, 76 bytes

for($a=$argn;~$c=$a[$i++];)stripos($a,$c)<strripos($a,$c)?:$r.=$c;echo$r[1];

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Java (OpenJDK 8), 133 131 bytes

s->{String t=s.toUpperCase();int[]i=t.chars().filter(c->t.indexOf(c)==t.lastIndexOf(c)).toArray();return(char)(i.length>1?i[1]:0);}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pyth, 17 bytes

Jrz1.x.(fq/JT1J1k

Test suite

Python 3 translation:
J=input().upper()
try:
    print([T for T in J if J.count(T)==1][1])
except:
    print()
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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