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?

3
  • Here stackoverflow.com/questions/14696584/java-loop-through-array Commented Dec 9, 2013 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? Commented Dec 9, 2013 at 16:58
  • All persons with the same number have the same value Commented Dec 9, 2013 at 17:05

3 Answers 3

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?.

Sign up to request clarification or add additional context in comments.

Comments

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()

4 Comments

or just use a set.. Checking if an object is in an array already every time can get expensive.
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 ^^
I get an array with names, but i only want to show how many different names there are as a number
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 ^^
0

Try:

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

uniqueList.size(); //Number of unique objects

2 Comments

(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.
(4) I missed this the first time: arrays don't have an asList method. The correct call is Arrays.asList(array).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.