title: Groovy: Optimize code to find duplicate elements
body: <p>I have <code>invoiceList</code> as below which is a <code>List&lt;Map&lt;String:String&gt;&gt;</code> and am trying to findout if all the invoices have same <code>SENDER_COUNTRY</code> and <code>CLIENT_COUNTRY</code> or not, if not it will add message to JSON array</p>
  
  <pre><code>[
      [INVOICE_DATE:20150617, INVOICE_NUMBER:617151,SENDER_COUNTRY:USA, CLIENT_COUNTRY:USA]
      [INVOICE_DATE:20150617, INVOICE_NUMBER:617152,SENDER_COUNTRY:CAD, CLIENT_COUNTRY:MEX]
      [INVOICE_DATE:20150617, INVOICE_NUMBER:617153,SENDER_COUNTRY:CAD, CLIENT_COUNTRY:MEX]
  ]
  
  JSONArray jsonArray = new JSONArray();
  def senderCountry = invoiceList[0]['SENDER_COUNTRY']
  def clientCountry  = invoiceList[0]['CLIENT_COUNTRY']
  invoiceList.each{ it -&gt;
  if(it['SENDER_COUNTRY'] != senderCountry)
    jsonArray.add((new JSONObject()).put("SENDER_COUNTRY","Multiple sender Countries Associated"));
   if(it['CLIENT_COUNTRY'] != clientCountry)
    jsonArray.add((new JSONObject()).put("CLIENT_COUNTRY","Multiple Client Countries Associated"));
  }
  </code></pre>
  
  <p>I feel this code can be refactored/optimized to a better version in Groovy, can someone please help me with it?</p>