<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="130074" data-post-id="130074">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="hauleth" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/120/18942_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  hauleth
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>It can be pretty slow as you are iterating through all elements twice. Instead it is better to use:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">inc = &amp; &amp;1 + 1
Enum.reduce(enumerable, %{}, &amp;Map.update(&amp;2, &amp;1, 1, inc))
</code></pre>
<p>But in this case if this is homework and you know that the constraints will always hold then XOR solution by the <a class="mention" href="/u/mudasobwa" rel="nofollow">@mudasobwa</a>. If you do not know whether it will integer-only or you do not know if there will be only one value that occurs odd number of times, then second solution with map (however I would use <code>Map.has_key?/2</code> or <code>Map.fetch/2</code> instead of <code>Map.get/2</code> as it is less quirky).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="130074" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/given-a-non-empty-list-of-integers-every-element-appears-twice-except-for-one-find-that-single-one/22728/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-130074" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="130074"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #21"></div>
  </section>
</div>
    <div class="postbit" id="130193" data-post-id="130193">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Ted" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Ted/120/15126_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Ted
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Agreed, that simple implementation of <code>frequencies</code> isn’t optimal.</p>
<p>Here’s how Clojure <a href="https://github.com/clojure/clojure/blob/clojure-1.9.0/src/clj/clojure/core.clj#L7123" rel="noopener nofollow ugc">actually does it</a>:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">(defn frequencies
  "Returns a map from distinct items in coll to the number of times
  they appear."
  {:added "1.2"
   :static true}
  [coll]
  (persistent!
   (reduce (fn [counts x]
             (assoc! counts x (inc (get counts x 0))))
           (transient {}) coll)))
</code></pre>
<p>So it looks like your approach and Clojure’s are pretty much the same, although in Clojure’s case it is manipulating a mutable map (the <code>(transient {})</code> part) and then rendering it immutable upon return (the <code>(persistent! ...)</code> part).</p>
<p>For those who are curious, <a href="https://clojure.org/reference/transients" rel="noopener nofollow ugc">here are more details</a> about Clojure’s transient data structures.</p>
<p>I’m not sure if Elixir has something equivalent, or whether that’s even desirable in Elixir land.</p>
<p>I like your <code>inc</code> function, by the way. <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>Clojure’s standard library offers <code>inc</code> and <code>dec</code> and thus they’re within easy reach.</p>
<p>It’s nice to see they’re so easily implemented in Elixir.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="130193" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/given-a-non-empty-list-of-integers-every-element-appears-twice-except-for-one-find-that-single-one/22728/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-130193" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="130193"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #22"></div>
  </section>
</div>
    <div class="postbit" id="130233" data-post-id="130233">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Ted" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Ted/120/15126_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Ted
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hmm, I just learned about the <code>:reduce</code> option in <code>for</code> comprehensions:</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://elixir.hexdocs.pm/Kernel.SpecialForms.html">
  <header class="source">

      <a href="https://elixir.hexdocs.pm/Kernel.SpecialForms.html" target="_blank" rel="noopener nofollow ugc">elixir.hexdocs.pm</a>
  </header>

  <article class="onebox-body">
    

<h3><a href="https://elixir.hexdocs.pm/Kernel.SpecialForms.html" target="_blank" rel="noopener nofollow ugc">Kernel.SpecialForms — Elixir v1.20.2</a></h3>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>Looks like my old, slow implementation of frequencies can change from this…</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">frequencies = fn(enumerable) -&gt;
  for {k, v} &lt;- Enum.group_by(enumerable, &amp;(&amp;1)), into: %{}, do: {k, length(v)}
end
</code></pre>
<p>…to this variation on <a class="mention" href="/u/hauleth" rel="nofollow">@hauleth</a>’s example:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">inc = &amp; &amp;1 + 1
frequencies = fn(enumerable) -&gt;
  for x &lt;- enumerable, reduce: %{} do
    acc -&gt; Map.update(acc, x, 1, inc)
  end
end
</code></pre>
<p><img src="https://forum.elixirforum.com/uploads/default/original/2X/d/d6aafab20efae12a589086d31e135401088e8cf1.gif?v=15" title=":icon_cool:" class="emoji emoji-custom only-emoji" alt=":icon_cool:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="130233" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/given-a-non-empty-list-of-integers-every-element-appears-twice-except-for-one-find-that-single-one/22728/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-130233" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="130233"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #23"></div>
  </section>
</div>
    <div class="postbit" id="130272" data-post-id="130272">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Andres" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Andres/120/15597_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Andres
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Awesome! Thanks so much for sharing! <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="130272" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/given-a-non-empty-list-of-integers-every-element-appears-twice-except-for-one-find-that-single-one/22728/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-130272" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="130272"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #24"></div>
  </section>
</div>
    <div class="postbit" id="130275" data-post-id="130275">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Ted" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Ted/120/15126_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Ted
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>And thank you for asking a related question, otherwise using <code>:reduce</code> wouldn’t have come to mind. <img src="https://forum.elixirforum.com/images/emoji/apple/rocket.png?v=15" title=":rocket:" class="emoji" alt=":rocket:" loading="lazy" width="20" height="20"></p>
<aside class="quote quote-modified" data-post="1" data-topic="22769">
  <div class="title">
    <div class="quote-controls"></div>
    <img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/andres/48/15597_2.png" class="avatar">
    <div class="quote-title__text-content">
      <a href="https://forum.elixirforum.com/t/count-the-number-of-non-numeric-characters-in-a-string-and-then-sum-their-values/22769" rel="nofollow">Count the number of non-numeric characters in a string and then sum their values</a> <a class="badge-category__wrapper " href="/c/questions-help/questions/53" rel="nofollow"><span data-category-id="53" style="--category-badge-color: #C14BFB; --category-badge-text-color: #000000; --parent-category-badge-color: #C14BFB;" data-parent-category-id="171" data-drop-close="true" class="badge-category --style-square --has-parent" title="Elixir Questions / Help"><span class="badge-category__name">Questions</span></span></a>
    </div>
  </div>
  <blockquote>
    Hello. 
Trying to solve this problem I wrote the following algorithm: 
string = "3340c9571y40m47ku49t9315mrvzqo667k36e"

# %{
#  "c" =&gt; 1,
#  "e" =&gt; 1,
#  "k" =&gt; 2,
#  "m" =&gt; 2,
#  "o" =&gt; 1,
#  "q" =&gt; 1,
#  "r" =&gt; 1,
#  "t" =&gt; 1,
#  "u" =&gt; 1,
#  "v" =&gt; 1,
#  "y" =&gt; 1,
#  "z" =&gt; 1
#  }
#  [1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1]
# Expected result =&gt; 14

def is_numeric?(char) do
    Regex.match?(~r/^\d+$/, char)
end

def total_non_numeric_chars(string) do
    String.codepoints(string)
    |&gt; Enum.redu…
  </blockquote>
</aside>
 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="130275" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/given-a-non-empty-list-of-integers-every-element-appears-twice-except-for-one-find-that-single-one/22728/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-130275" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="130275"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>