aspose file tools*
The moose likes Beginning Java and the fly likes How to find duplicates in an Array object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to find duplicates in an Array object" Watch "How to find duplicates in an Array object" New topic
Author

How to find duplicates in an Array object

Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
Hello All,

I have this Array of an object type where the object contains three elements that constructs as a key.

SomeData[] arrSomeData

I will have multiple occurences of this object. I need to make sure there are no duplicates based of this key value. If such duplicate is found an exception is raised. I can do it using nested loops of itself. Get each of these three values and compare it with corresponding value. Use separate boolean variable if a match found for each of these three values individually and use another boolean when all three matches.

for (int i = 0; i < arrSomeData.length; i++) {
for (int j = 0; j < arrSomeData.length; j++) {

if (arrSomeData[i].getKey1().equalsIgnoreCase(arrNRCPromoData[i].getChargeCode()))
{

boolMatchChargeCode = true;

}
if (arrCharge[j].getOfferId() == (arrNRCPromoData[i].getOfferId())) {

boolMatchOfferId = true;

}
if (arrCharge[j].getOfferInstanceId().equalsIgnoreCase(arrNRCPromoData[i].getOfferInstanceId())) {

boolMatchOfferInstanceId = true;

}
if (boolMatchChargeCode == true && boolMatchOfferId == true && boolMatchOfferInstanceId == true)
{


Wondering if there is better way of doing this?


Thanks
Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
Oops! accidentally hit the enter key to my posting before finishing.

Hello All,

I have this Array of an object type where the object contains three elements that constructs as a key.

SomeData[] arrSomeData

I will have multiple occurences of this object. I need to make sure there are no duplicates based of this key value. If such duplicate is found an exception is raised. I can do it using nested loops of itself. Get each of these three values and compare it with corresponding value. Use separate boolean variable if a match found for each of these three values individually and use another boolean when all three matches.

for (int i = 0; i < arrSomeData.length; i++) {
for (int j = 0; j < arrSomeData.length; j++) {

if (arrSomeData[i].getKey1().equalsIgnoreCase(arrSomeData[j].getKey1()))
{

boolMatchKey1 = true;

}
if (arrSomeData[i].getKey2().equalsIgnoreCase(arrSomeData[j].getKey2()))
{

boolMatchKey2 = true;

}

if (arrSomeData[i].getKey3().equalsIgnoreCase(arrSomeData[j].getKey3()))
{

boolMatchKey3 = true;

}

if (boolMatchKey1 == true && boolMatchKey2 == true && boolMatchKey3 == true)
{
boolMatch = true;
}
. . .

Wondering if there is better way of doing this?


Thanks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

you might be able to hash the three key elements together in such a way as to make a key, and then use a hashmap instead of an Array.

note: you can always go back and edit your own posts, rather than creating a (mostly) duplicate one by clicking the pencil/paper icon above your post.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
Thanks Fred for your suggestion. I will try it out using HashMap. I would have in the first place if had to only deal with two instead of three values to deal with. Didn't want to append two of these to become a value and the other to be a key of the HashMap object. Thought it would be overkill.
Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
Here's what I have as a sample code using HashMap -


Output -

Duplicate found -> 04565ABC
Duplicate found -> 0777ABC
The value of boolDup -> true
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find duplicates in an Array object