title: For loop in dictionary in XQuery
body: <p>I am trying to loop over items and output number of occurrences for each one, but it outputs the same dictionary a number of times equal to the number of items. Is there a way same as using parentheses to just make the loops for <code> let</code>  statements. I know that XQuery is a functional language and that I can use <code> group by</code>  but it is not supported in marklogic.</p>
  <pre><code>xquery version &quot;1.0-ml&quot;;
  let $dict := map:map()
  for $i in ('book1','book2','book2')
  let $n := (if (map:contains($dict, $i)) then (map:get($dict, $i)) else (0))
  let $dict1 := map:put($dict, $i, 1 + $n)
  return $dict
  </code></pre>
  <p>Output:</p>
  <pre><code>&lt;map:map&gt;
  &lt;map:entry key=&quot;book1&quot;&gt;
  &lt;map:value xsi:type=&quot;xs:integer&quot;&gt;1&lt;/map:value&gt;
  &lt;/map:entry&gt;
  &lt;map:entry key=&quot;book2&quot;&gt;
  &lt;map:value xsi:type=&quot;xs:integer&quot;&gt;2&lt;/map:value&gt;
  &lt;/map:entry&gt;
  &lt;/map:map&gt;
  map as 
  XML
  &lt;map:map&gt;
  &lt;map:entry key=&quot;book1&quot;&gt;
  &lt;map:value xsi:type=&quot;xs:integer&quot;&gt;1&lt;/map:value&gt;
  &lt;/map:entry&gt;
  &lt;map:entry key=&quot;book2&quot;&gt;
  &lt;map:value xsi:type=&quot;xs:integer&quot;&gt;2&lt;/map:value&gt;
  &lt;/map:entry&gt;
  &lt;/map:map&gt;
  map as 
  XML
  &lt;map:map&gt;
  &lt;map:entry key=&quot;book1&quot;&gt;
  &lt;map:value xsi:type=&quot;xs:integer&quot;&gt;1&lt;/map:value&gt;
  &lt;/map:entry&gt;
  &lt;map:entry key=&quot;book2&quot;&gt;
  &lt;map:value xsi:type=&quot;xs:integer&quot;&gt;2&lt;/map:value&gt;
  &lt;/map:entry&gt;
  &lt;/map:map&gt;
  </code></pre>