Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to find duplicates in an Array object

 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I have as a sample code using HashMap -


Output -

Duplicate found -> 04565ABC
Duplicate found -> 0777ABC
The value of boolDup -> true
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic