0

I have an array which looks like this:

  array = { Person1, Person2, Person3, Person1, Person2, Person3};

Now how can I get an int with the total count of unique Persons? That means a Person which was already counted, shouldn't be counted again.

In the give example above should return the int value of 3

  int uniq = 3;

How can I do this task?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Deniz Celebi
  • 888
  • 4
  • 14
  • 24
  • Here http://stackoverflow.com/questions/14696584/java-loop-through-array – Sushan Ghimire Dec 09 '13 at 16:54
  • Before we answer, how do you know the "persons" are equal? Are they the same reference (so that `array[0] == array[3]` would be true, in the above example)? Or have you redefined `equals` for your `Person` class? Or are you using some other method to test whether two persons are the same? – ajb Dec 09 '13 at 16:58
  • All persons with the same number have the same value – Deniz Celebi Dec 09 '13 at 17:05

3 Answers3

0

the easiest way is to avoid doublon when you add the person in your array. To do that, when you add a Person in the array, you go through all the array and verify that you don't already have it :)

If you need to have the doublon in the list, then you should create a tempArray and only add the different person in this new array like described in the other method, so in the end you will have your good number in the tempArray.size()

Clad Clad
  • 2,653
  • 1
  • 20
  • 32
  • or just use a set.. Checking if an object is in an array already every time can get expensive. – turbo Dec 09 '13 at 16:59
  • I actually didn't know about Brianjs's method but that's better than mine ^^ but actually the real thing is why does he have to have doublon in his list ^^ – Clad Clad Dec 09 '13 at 17:01
  • I get an array with names, but i only want to show how many different names there are as a number – Deniz Celebi Dec 09 '13 at 17:09
  • Yes I understand that but do you have a use of having them in double ? or it's just a pain and in this case you should avoid adding double from the beginning :) at least that's my opinion ^^ – Clad Clad Dec 09 '13 at 18:12
0

Try:

List<Person> uniqueList = new ArrayList<Person>(new LinkedHashSet<Person>( array.asList() ).sort() );

uniqueList.size(); //Number of unique objects
Brianjs
  • 2,079
  • 2
  • 20
  • 32
  • 1
    (1) `LinkedHashSet` doesn't have a `sort` method; (2) you couldn't even do a `sort` if the element types aren't `Comparable`, and we have no idea whether the element type has ordering functions defined; (3) if all he wants is the size, then `sort` would be pointless anyway. – ajb Dec 09 '13 at 17:02
  • (4) I missed this the first time: arrays don't have an `asList` method. The correct call is `Arrays.asList(array)`. – ajb Dec 09 '13 at 17:08
0

If all you want is the size, this is a correct version of one of the other answers:

Set<Person> set = new HashSet<Person>(Arrays.asList(array));
int count = set.size();

This assumes that either your Person type has an equals method that will tell you whether two persons are the same, or if there's no equals, the default equals is OK (the default returns true if the references are equal).

P.S.: if you've defined your own Person in equals, you must also define hashCode or else this solution won't work. hashCode must be defined so that if two Persons are equal then they also have the same hash code. See What issues should be considered when overriding equals and hashCode in Java?.

Community
  • 1
  • 1
ajb
  • 31,309
  • 3
  • 58
  • 84