<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Sorting array of values and counting unique values occurrences in Developers Questions</title>
    <link>https://community.esri.com/t5/developers-questions/sorting-array-of-values-and-counting-unique-values/m-p/1573856#M7393</link>
    <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;I am struggling with creating a block of code that would sort my array, count unique values, and return them in a text string.&lt;/P&gt;&lt;P&gt;For example: I have this array of fruits:&lt;/P&gt;&lt;P&gt;["Apples", "Bananas", "Berry", "Bananas", "Apples", "Bananas"]&lt;/P&gt;&lt;P&gt;What I would like to get is this output:&lt;/P&gt;&lt;P&gt;Apples x 2, Bananas x 3, Berry x 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm not sure how to get around to this, if it was Feature Set I would use the GroupBy function...&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 09 Jan 2025 11:17:30 GMT</pubDate>
    <dc:creator>Julia8</dc:creator>
    <dc:date>2025-01-09T11:17:30Z</dc:date>
    <item>
      <title>Sorting array of values and counting unique values occurrences</title>
      <link>https://community.esri.com/t5/developers-questions/sorting-array-of-values-and-counting-unique-values/m-p/1573856#M7393</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;I am struggling with creating a block of code that would sort my array, count unique values, and return them in a text string.&lt;/P&gt;&lt;P&gt;For example: I have this array of fruits:&lt;/P&gt;&lt;P&gt;["Apples", "Bananas", "Berry", "Bananas", "Apples", "Bananas"]&lt;/P&gt;&lt;P&gt;What I would like to get is this output:&lt;/P&gt;&lt;P&gt;Apples x 2, Bananas x 3, Berry x 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm not sure how to get around to this, if it was Feature Set I would use the GroupBy function...&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jan 2025 11:17:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/developers-questions/sorting-array-of-values-and-counting-unique-values/m-p/1573856#M7393</guid>
      <dc:creator>Julia8</dc:creator>
      <dc:date>2025-01-09T11:17:30Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting array of values and counting unique values occurrences</title>
      <link>https://community.esri.com/t5/developers-questions/sorting-array-of-values-and-counting-unique-values/m-p/1573866#M7394</link>
      <description>&lt;P&gt;What language?&amp;nbsp; In python, you can use a Counter&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from collections import Counter
x = Counter(["Apples", "Bananas", "Berry", "Bananas", "Apples", "Bananas"])
print(x)&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 09 Jan 2025 11:39:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/developers-questions/sorting-array-of-values-and-counting-unique-values/m-p/1573866#M7394</guid>
      <dc:creator>MikeMillerGIS</dc:creator>
      <dc:date>2025-01-09T11:39:42Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting array of values and counting unique values occurrences</title>
      <link>https://community.esri.com/t5/developers-questions/sorting-array-of-values-and-counting-unique-values/m-p/1573888#M7395</link>
      <description>&lt;P&gt;In JavaScript/Typescript you might take advantage of the reduce method (granted that 1 liner in python is freaking cool&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/188597"&gt;@MikeMillerGIS&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Here are two possibilities, one uses an NPM library called lodash the other looks is plain JS/Typescript (no new library needed to install)&lt;BR /&gt;&lt;BR /&gt;JavaScript/Typescript using NPM library &lt;STRONG&gt;lodash&amp;nbsp; &lt;/STRONG&gt;(the _ is the import name for lodash in this case)&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;import _ from "lodash";

function summarizeArray(arr: string[]): string {
  // Step 1: Count occurrences using _.countBy
  const counts = _.countBy(arr);

  // Step 2: Sort keys alphabetically and format the result
  const summary = _(counts)
    .toPairs() // Convert object to array of [key, value] pairs
    .sortBy(0) // Sort by the key (fruit name)
    .map(([fruit, count]) =&amp;gt; `${fruit} x ${count}`) // Format each pair
    .join(", "); // Join into a single string

  return summary;
}

// Example usage
const fruits = ["Apples", "Bananas", "Berry", "Bananas", "Apples", "Bananas"];
const result = summarizeArray(fruits);
console.log(result); // Output: "Apples x 2, Bananas x 3, Berry x 1"&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;JavaScript/TypeScript with no additional library needed&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;function summarizeArray(arr: string[]): string {
  // Step 1: Count occurrences of each item
  const counts = arr.reduce((acc, fruit) =&amp;gt; {
    acc[fruit] = (acc[fruit] || 0) + 1;
    return acc;
  }, {} as Record&amp;lt;string, number&amp;gt;);

  // Step 2: Sort the keys alphabetically
  const sortedKeys = Object.keys(counts).sort();

  // Step 3: Map to the desired format
  const summary = sortedKeys.map(key =&amp;gt; `${key} x ${counts[key]}`).join(", ");

  return summary;
}

// Example usage
const fruits = ["Apples", "Bananas", "Berry", "Bananas", "Apples", "Bananas"];
const result = summarizeArray(fruits);
console.log(result); // Output: "Apples x 2, Bananas x 3, Berry x 1"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jan 2025 13:55:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/developers-questions/sorting-array-of-values-and-counting-unique-values/m-p/1573888#M7395</guid>
      <dc:creator>TimWestern</dc:creator>
      <dc:date>2025-01-09T13:55:19Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting array of values and counting unique values occurrences</title>
      <link>https://community.esri.com/t5/developers-questions/sorting-array-of-values-and-counting-unique-values/m-p/1573908#M7396</link>
      <description>&lt;P&gt;not to leave numpy out&lt;/P&gt;&lt;LI-CODE lang="python"&gt;a = ["Apples", "Bananas", "Berry", "Bananas", "Apples", "Bananas"]

import numpy as np
def uniq(a):
    """`a` is your array."""
    u, cnts = np.unique(a, return_counts=True)
    st = ""
    for cnt, v in enumerate(u):
        st += "{}, {}  ".format(v, cnts[cnt])
    return st.strip()
    

uniq(a)
'Apples, 2  Bananas, 3  Berry, 1'&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 09 Jan 2025 14:18:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/developers-questions/sorting-array-of-values-and-counting-unique-values/m-p/1573908#M7396</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2025-01-09T14:18:40Z</dc:date>
    </item>
  </channel>
</rss>

