Reputation: 21
I'm looking to find the number of duplicate pairs in a Java ArrayList.
I can work it out on paper but I don't know if there is some form of mathematical formula for working this out easily as I'm trying to avoid nested for loops in my code.
An example using the data set [2,2,3,2,2]: 0:1, 0:3, 0:4, 1:3, 1:4, 3:4. So the answer is six duplicate pairs?
Upvotes: 2
Views: 1912
Reputation: 51945
If you want to avoid nested loops (at the expense of having 2 loops), you could:
Doing a sort like ElKamina suggests would allow for some optimization on this method.
Upvotes: 2
Reputation: 198103
Using Guava, if your elements were String
s:
Multiset<String> multiset = HashMultiset.create(list);
int pairs = 0;
for(Multiset.Entry<String> entry : multiset.entrySet()) {
pairs += IntMath.binomial(entry.getCount(), 2);
}
return pairs;
That uses Guava's Multiset
and math utilities.
Upvotes: 0
Reputation: 6322
You just need to count how many times each number appears (I would go with a map here) and calculate 2-combinations ( http://en.wikipedia.org/wiki/Combination ) of that count for each number with a count > 1.
So basically you need a method to calculate n!/k!(n-k)!
with k
being 2 and n
being the count.
Taking your example [2,2,3,2,2], the number 2 appears 4 times, so the math would go:
4!/2!(4-2)! = 24/4 = 6 --> 6 pairs
If you don't want to implement the factorial function, you can use the ArithmeticUtils from Apache Commons, they already have the factorial implemented.
Upvotes: 4
Reputation: 7807
Sort the numbers first. Later, if there k
copies of a given number, there will be k*(k-1)/2
pairs from that number. Now sum it over all the numbers.
Upvotes: 0