• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to find duplicates in an Array object

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic