Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I assume you can use additional libraries, this solution is based on that. Pull in <a href="https://code.google.com/p/guava-libraries/" rel="nofollow">Guava</a> and make use of a <a href="http://guava-libraries.googlecode.com/svn/tags/release03/javadoc/com/google/common/collect/Multimap.html" rel="nofollow"><code>MultiMap</code></a>:</p> <pre><code>LinkedListMultimap&lt;String, String&gt; mappedPersons = LinkedListMultimap.create(); for (int i = 0; i &lt; IDArray.size(); i++) { IdenPerson person = IDArray.get(i); mappedPersons.put(person.name, person.dis); } List&lt;IdenPerson&gt; uniques = new ArrayList&lt;IdenPerson&gt;(mappedPersons.size()); for(int i = 0 ; i &lt; IDArray.size(); i++) { if(mappedPersons.get(IDArray.get(i).name).size() == 1) { uniques.add(IDArray.get(i)); } } </code></pre> <p>Code needs cleanup, and can be optimized, but you get the gist.</p> <p>On a side note, you should definitely follow better naming standards and use proper accessor/mutators for your Java objects.</p> <p><em>New side note: next time, please indicate if the question is related to an assignment or homework. Giving near complete solutions in such cases does not help you in the long run</em></p> <hr> <p>If you cannot pull in other libraries for any reason, you can accomplish the task in the same way, by replacing the multimap with a map/list combo:</p> <pre><code>Map&lt;String, Set&lt;String&gt;&gt; mappedPersons = new HashMap&lt;String, Set&lt;String&gt;&gt;(IDArray.size()); for (int i = 0; i &lt; IDArray.size(); i++) { IdenPerson person = IDArray.get(i); List&lt;String&gt; diseases = mappedPersons.get(person.name); if(diseases == null) { diseases = new HashSet&lt;String&gt;(4); mappedPersons.put(person.name, diseases); } diseases.add(person.dis); } List&lt;IdenPerson&gt; uniques = new ArrayList&lt;IdenPerson&gt;(mappedPersons.size()); for(int i = 0 ; i &lt; IDArray.size(); i++) { if(mappedPersons.get(IDArray.get(i).name).size() == 1) { uniques.add(IDArray.get(i)); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload