<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.appian.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Save first occurence from an array of elements</title><link>https://community.appian.com/discussions/f/rules/37739/save-first-occurence-from-an-array-of-elements</link><description>Hello, I need to create rule that takes an array of dictionaries in input and if multiple of these elements have the same &amp;quot;id&amp;quot; value, I want to take only the first one in a separate list: 
 ri!list: 
 
 the output should be the following: 
 local!output</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: Save first occurence from an array of elements</title><link>https://community.appian.com/thread/141849?ContentTypeID=1</link><pubDate>Fri, 18 Oct 2024 16:28:15 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:9d5cb904-b98d-4e88-b058-407967572531</guid><dc:creator>Chiara Gambone</dc:creator><description>&lt;p&gt;Thank you Mike&lt;/p&gt;
&lt;div style="display:none;position:absolute;"&gt;&lt;/div&gt;
&lt;div style="display:none;position:absolute;"&gt;&lt;/div&gt;
&lt;div style="display:none;position:absolute;"&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Save first occurence from an array of elements</title><link>https://community.appian.com/thread/141779?ContentTypeID=1</link><pubDate>Thu, 17 Oct 2024 06:24:28 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:956bc331-e747-4f76-96d7-60b710134577</guid><dc:creator>Soma</dc:creator><description>&lt;p&gt;You can achieve it using foreach(), index() and wherecontains()&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;a!localVariables(
  local!list: {
    {
      output: &amp;quot;OK&amp;quot;,
      id: &amp;quot;abcd&amp;quot;,
      description: &amp;quot;hello&amp;quot;
    },
    { output: &amp;quot;OK&amp;quot;, id: &amp;quot;abcd&amp;quot;, description: &amp;quot;hi&amp;quot; },
    { output: &amp;quot;OK&amp;quot;, id: &amp;quot;yxz&amp;quot;, description: &amp;quot;bye&amp;quot; },
    {
      output: &amp;quot;OK&amp;quot;,
      id: &amp;quot;yxz&amp;quot;,
      description: &amp;quot;goodnight&amp;quot;
    },
    { output: &amp;quot;OK&amp;quot;, id: &amp;quot;opt&amp;quot;, description: &amp;quot;ok&amp;quot; }
  },
  local!uniqueIds: union(local!list.id, local!list.id),
  a!forEach(
    items: local!uniqueIds,
    expression: index(
      index(
        local!list,
        wherecontains(fv!item, touniformstring(local!list.id)),
        null
      ),
      1,
      null
    )
  )
)&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Save first occurence from an array of elements</title><link>https://community.appian.com/thread/141757?ContentTypeID=1</link><pubDate>Wed, 16 Oct 2024 17:12:08 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:dd7b4b8f-ecc4-4e01-8c1e-88ffce61af54</guid><dc:creator>Mike Schmitt</dc:creator><description>&lt;p&gt;I usually tackle this by breaking it down into pseudocode that describe successive steps of the process, then replacing those with local variables that handle that step (and abstract the functionality behind that variable, which helps in dev and debugging etc).&lt;/p&gt;
&lt;p&gt;Here goes my quick &amp;quot;first pass&amp;quot;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;take the initial array and pass the IDs through my &amp;quot;distinct&amp;quot; helper rule to get a list of unique IDs&amp;nbsp; (&amp;quot;distinct&amp;quot; is usually just an expression rule that calls union() on the input rule input, repeated)&lt;/li&gt;
&lt;li&gt;iterate over that list of IDs and get lists of indexes of the index locations in the initial dictionary for the current id (&amp;quot;wherecontains()&amp;quot;)&lt;/li&gt;
&lt;li&gt;(can possibly be done at the same moment as step 2) - narrow down to just the first index for each item&lt;/li&gt;
&lt;li&gt;call index() on the original dictionary array, passing in the narrowed-down index values, to produce the initial list entries for each ID&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Edit: merely for the sake of curiosity, I timed myself at 3 minutes, 4 seconds to translate the above steps into code and whip up this:&lt;br /&gt;&lt;pre class="ui-code" data-mode="java"&gt;a!localVariables(
  
  local!initialDict: {
    {output: &amp;quot;OK&amp;quot;, id: &amp;quot;abcd&amp;quot;, description: &amp;quot;hello&amp;quot;},
    {output: &amp;quot;OK&amp;quot;, id: &amp;quot;abcd&amp;quot;, description: &amp;quot;hi&amp;quot;},
    {output: &amp;quot;OK&amp;quot;, id: &amp;quot;yxz&amp;quot;, description: &amp;quot;bye&amp;quot;},
    {output: &amp;quot;OK&amp;quot;, id: &amp;quot;yxz&amp;quot;, description:&amp;quot;goodnight&amp;quot;},
    {output: &amp;quot;OK&amp;quot;, id: &amp;quot;opt&amp;quot;, description: &amp;quot;ok&amp;quot;}
  },

  local!uniqueIds: rule!RULE_General_distinct(array: local!initialDict.id),  /* just does a &amp;quot;union()&amp;quot; on ri!array, on itself, quickly eliminating duplicates */
  
  local!indices: a!forEach(
    local!uniqueIds,
    index(
      wherecontains(fv!item, touniformstring(local!initialDict.id)),
      1,
      {}
    )
  ),
  
  index(
    local!initialDict,
    local!indices
  )
)&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" alt=" " src="/resized-image/__size/640x480/__key/communityserver-discussions-components-files/15/pastedimage1729099018817v1.png" /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>