#!/usr/bin/perl
$^W=1;
use strict;

# various details/background at end

$ENV{LC_ALL}='C';

local $/=\1; # read 1 character at a time

# vi(1) :se tabstop=4

my %h=();		# hash by letters seen
my $p=26;		# number of possible letters remaining
my @seen=();	# letters seen in order first seen

while(<>){
	/[a-z]/ or die("$0: illegal input character: $_, aborting\n");
	if(!exists($h{$_})){
		$h{$_}=1;
		push(@seen,$_);	# track letter seen
	}elsif($h{$_}){
		# just saw letter for 2nd time
		--$h{$_};
		--$p or			# decrement possible letters
			exit(1);	# all 26 seen twice, nothing left to do,
						# return non-zero to indicate no match
	}
	#else{
		# Already seen twice, uninteresting, nothing to do.
		# For efficency, since input may be huge,
		# we do no extra processing here,
		# but rather later eliminate letters seen more than twice
	#}
}
# exhausted input, do we have a match?
for (@seen){
	$h{$_} or next;	# skip if seen more than once
	# exactly once:
	print "$_";
	exit;
}
exit(1);	# none seen exactly once

# Based losely upon:
# https://www.youtube.com/watch?v=5co5Gvp_-S0
# presumably from / asked on/by codesignal.com, Amazon, and/or Google
# And with some adjustments,
# notably not limited length string given to function,
# but rather stdin of potentially arbitrary amount of input data
# essentially input of ASCII characters a-z only,
# output fist character that's seen only once,
# exit non-zero and no output if no qualifying charcter seen,
# diagnostic to stderr and non-zero exit if illegal input seen
