title: Print / Find the first character (with the LOWEST occurrence) in a non-empty string and order is important
body: <p><strong>BASH</strong> GNU bash, version <strong>4.2.46</strong>(2)-release (x86_64-redhat-linux-gnu)</p>
  
  <p>Given a string <strong>str</strong>, which can store only any lower, UPPER or number values only.</p>
  
  <p>How can I find the first character (<strong>with the LOWEST occurrence</strong>) in a <strong>non-empty</strong> string <code>str</code>? The focus of the problem is to print letter '<strong>z</strong>' if the script is this (as fast as possible, without getting any errors if the data is in a string or a file): <a href="https://repl.it/@asangal/find1stleastoccurrencecharmaintainorderanyleastsize" rel="nofollow noreferrer">https://repl.it/@asangal/find1stleastoccurrencecharmaintainorderanyleastsize</a> </p>
  
  <p><br/><br/></p>
  
  <p><strong>or Sample example</strong> values of <code>str</code>:</p>
  
  <p><code>str=aa</code>, output should be '<strong>a</strong>'  (as 'a' is the only one char in string - occurred 2 times)</p>
  
  <p><code>str=aa1</code>, output should be '<strong>1</strong>' (as '1' is the first character with lowest occurrence count of 1)</p>
  
  <p><code>str=aa1c1deef</code>, output should be '<strong>c</strong>' (as 'c' came before 'd' and both had 1 as lowest occurrence count of 1)</p>
  
  <p><code>str=abcdeeddAbac</code>, output should be '<strong>A</strong>' (as 'A' is the first character with lower occurrence count of 1)</p>
  
  <p><code>str=abcdeeddAbacA</code>, output should be '<strong>a</strong>' (as 'a' is the first character with lower occurrence count of 2)</p>
  
  <p><code>str=abcdeeddAbacAabc</code>, output should be '<strong>e</strong>' (as 'e' is the first character with lower occurrence count of 2)</p>
  
  <p><br/><br/>
  <strong>Other large size example value</strong> can be:</p>
  
  <p><code>str=axavzzzfdfdsldfnasdlkfjasdlkfjaslkfjasldkfjaslfjlasjkflasdkjfasdlfjasdljfasdkjfgio23yoryoiasyfoiywoerihlkdfhlaskdnkasdnvxcnvjzxkiivhaslyqwoyroiqwyroqwroqwlkasddlkkhaslkfjasdldkfjalsdkfashoqwiyroiqwyroiqwhrkjhajkdfhaslfkhasldkfh</code>, output should be '<strong>g</strong>' (as 'g' is the first character with lowest occurrence count of 1)</p>
  
  <p><br/><br/>
  <strong>Constraints / Context:</strong></p>
  
  <ol>
  <li><strong>Value</strong> can be a set of either a <strong>lower</strong>, <strong>UPPER</strong> or a <strong>Number</strong></li>
  <li>String will always be <strong>non-empty</strong>; we can ignore any space type characters  in the value for now.</li>
  <li>Find the first letter(<strong>[a-zA-Z0-9]</strong>) present in the <code>str</code> string <strong>with lowest occurrence count</strong>.</li>
  <li>If possible, I <strong>don't want to use</strong> any Statements (ex: if-then-else), loops (For/While) or user defined functions. <em>Use of commands, library functions</em> (if any available to a user out of the box) is <strong>OK</strong>. </li>
  </ol>
  
  <p><strong>PS:</strong> I know system level commands does call all these things behind the scene, but I'm looking for <strong>minimal code</strong> if possible <strong>at command line</strong> i.e. <strong>$</strong> prompt.</p>
  
  <p>I tried the following ugly-looking <strong>non-one-liner</strong> attempt as listed below, <em>here I have the <strong>for</strong> loop <strong>which I want to avoid</strong> if possible</em> and the <code>sort</code> command is helping but also making me loose the order and doesn't cover all the conditions.</p>
  
  <p><em>I don't like my current attempt that I have listed below, but seems like I'm close.</em></p>
  
  <pre><code>str="axavzzzfdfdsldfnasdlkfjasdlkfjaslkfjasldkfjaslfjlasjkflasdkjfasdlfjasdljfasdkjfgio23yoryoiasyfoiywoerihlkdfhlaskdnkasdnvxcnvjzxkiivhaslyqwoyroiqwyroqwroqwlkasddlkkhaslkfjasdldkfjalsdkfashoqwiyroiqwyroiqwhrkjhajkdfhaslfkhasldkfh";
  for char in $(echo $str | sed "s/\(.\)/\1\n/g" | grep .| tr '\012' ' '); 
  do
      echo -n "$char=$(echo ${str} | sed "s/\(.\)/\1\n/g" | grep . | grep -c $char)";echo;
  done | sort -u
  </code></pre>
  
  <p>I believe it's possible to achieve what I'm looking for as a <strong>One-liner</strong> (i.e. by using bunch of common Linux commands and pipes <strong>|</strong>) in BASH; just wanted to pick your brain! I know there are better <strong>shell</strong> experts than me.</p>
  
  <p>Most of the solutions I found online don't maintain the order (which is important to me) and just give the highest/lower occurrence/count of a character.</p>