Answers.Body: <p>You could use Craft's <a href="http://buildwithcraft.com/docs/templating/filters#group" rel="nofollow"><code>group</code></a> filter:</p>
  
  <pre><code>{% set files = craft.entries.section("downloads") | group('fileDate.year'} %}
  {% set dropdown = false %}
  
  {% for year, filesInYear in files %}
      {% if filesInYear|length &gt; 1 %}
          {% set dropdown = true %}
      {% endif %}
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-07T23:33:42.887Z
Answers.LastActivityDate: 2015-03-07T23:33:42.887Z
Answers.UserDisplayName: Michael Rog
Answers.Body: <p>This can be accomplished by creating an array of the entry years. Inside the loop that's building the array, a conditional checks if the year already exists in the array... if the year exists, a variable is set to TRUE which you can use in your conditional later down on the page. </p>
  
  <pre><code>{% set files = craft.entries.section("downloads") %}
  {% set dropdown = 0 %}
  {% set yearArray = [] %}
  
  {% for entry in files %}
      {% set year = entry.fileDate | date('Y') %}
      {% if year in yearArray %}
          {% set dropdown = 1 %}
      {% endif %}
      {% set yearArray = yearArray | merge([year]) %}
  {% endfor %}
  
  {% if dropdown %}
      Do this
  {% else %}
      Do this
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-05T23:10:29.347Z
Answers.LastActivityDate: 2015-03-05T23:10:29.347Z
Answers.UserDisplayName: Anna_MediaGirl
Body: <p>How do I loop through entries and set a variable if there are any duplicates in the data I'm checking? </p>
  
  <p>In my case, I need to check if there are more than one entry per year. The variable I'm setting will be used in a conditional later in the template.</p>
  
Tags: <templating><forloop>
Title: How do I set a variable in Twig if a value exists more than once in a loop?
ThreadText: # How do I set a variable in Twig if a value exists more than once in a loop?
  
  (Asked by: username_0 on 2015-03-05)
  
  How do I loop through entries and set a variable if there are any duplicates in the data I'm checking?
  
  In my case, I need to check if there are more than one entry per year. The variable I'm setting will be used in a conditional later in the template.
  
  **Tags:** templating, forloop
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2015-03-05. Score: 11 | Accepted answer
  
  This can be accomplished by creating an array of the entry years. Inside the loop that's building the array, a conditional checks if the year already exists in the array... if the year exists, a variable is set to TRUE which you can use in your conditional later down on the page.
  
  ```
  {% set files = craft.entries.section("downloads") %}
  {% set dropdown = 0 %}
  {% set yearArray = [] %}
  
  {% for entry in files %}
      {% set year = entry.fileDate | date('Y') %}
      {% if year in yearArray %}
          {% set dropdown = 1 %}
      {% endif %}
      {% set yearArray = yearArray | merge([year]) %}
  {% endfor %}
  
  {% if dropdown %}
      Do this
  {% else %}
      Do this
  {% endif %}
  
  ```
  
  
  ---
  
  ### Answer by username_1 on 2015-03-07. Score: 4
  
  You could use Craft's [`group`](http://buildwithcraft.com/docs/templating/filters#group) filter:
  
  ```
  {% set files = craft.entries.section("downloads") | group('fileDate.year'} %}
  {% set dropdown = false %}
  
  {% for year, filesInYear in files %}
      {% if filesInYear|length > 1 %}
          {% set dropdown = true %}
      {% endif %}
  {% endfor %}
  
  ```
  
  
  ---
  
  