Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I check for duplicates in an ArrayList?

Status
Not open for further replies.

neeljoshi86

Technical User
Mar 5, 2009
3
0
0
GB
Hi there. I have an ArrayList with a bunch of String values in it. I want to be able to check if a particular string appears X amount of times.

To add some context to the dilemma, the strings are date/times (in the format DD/MM/YYYY-HHMM) for which certain attendees are available for a meeting. For example, if two attendees are available on May 3rd 2004 at 9am, the ArrayList will be [03/05/2004-0900,03/05/2004-0900]. I want to be able to check through the arraylist and find out if the same string appears the same amount of times as there are attendees (meaning all attendees are free at that time).

My pseudo code for this is:

While there are still elements in the ArrayList {
Store the first element
for each element in the arraylist {
if it equals the stored element {
matchFound++
}
if matchFound = number of attendees {
return
}
}
for each matchFound {
remove the stored element from the ArrayList
}
}



Actual Code:

// While there are still elements in allResponses
while (x < allResponses.size()) {

// Store the first meeting response in list
String[] compare = allResponses.get(0);
int matchFound = 0;

for (int i=0; i<allResponses.size(); i++) {
if (compare.equals(allResponses.get(i))) {
matchFound++;
}

// We have an agreement between all attendees
if (matchFound == attendees.length) { return;
}
}

for (int p=0; p<matchFound; p++) {
allResponses.remove(compare);
}
}


I find this works until the second element is compared to the stored element. The first if statement inside the for loop doesn't think that the second element in the ArrayList is equal to the first?! Not sure what to do. I have tried toString(), converting to array before comparisons, == and .equals().

Any help is much appreciated!
 
Some ideas:

- Use the inedxOf method of the ArrayList to check the number of elements
- In your code you're comparing a String[] with a String

Cheers,
Dian
 
Make an Attendee Class. The class can hold name, number, etc. and available time. Give the Attendee class a method like
public boolean isAvailable(Date meetingDate){
...
}

Keep the Attendee Object in a Collection (Like ArrayList) and go thru that list calling each ones isAvailable method for the desired date. If any return false, not everyone is available.

pseudo
boolean allAvailable = true;
while(list has more attendees && allAvailable) {
allAvailable = nextAttendee.isAvailable();
}

now check allAvailable
 
Thanks guys. From what Dian had suggested I realised that earlier in my code I had defined allResponses as this:

ArrayList<String[]> allResponses = new ArrayList<String[]>();

Once I had changed this to:

ArrayList<String> allResponses = new ArrayList<String>();

I was then effectivley comparing a String to a String rather than an Array to a String.

I do like the idea you mentioned though djsiders - I might create a new class anyway. Seems a lot neater.

Many thanks!

Neel
 
You can also use HashSet to quickly remove the duplicates:

Code:
// You have your array
ArrayList someArrayList = new ArrayList();

// Now let's remove the duplicates by using HashSet
HashSet hashSet = new HashSet(someArrayList);
someArrayList.clear();
someArrayList.addAll(hashSet);
 
I don't think the OP wants to remove duplicates, but to count them ...

Cheers,
Dian
 
Actually... you can do this then...

Code:
int originalSize = someArrayList.size();
HashSet hashSet = new HashSet(someArrayList);
someArrayList.clear();
someArrayList.addAll(hashSet);

int numOfDuplicates = someArrayList.size();
 
okay... i am typing too fast. Sorry

Code:
int originalSize = someArrayList.size();

HashSet hashSet = new HashSet(someArrayList);
someArrayList.clear();
someArrayList.addAll(hashSet);

int withoutDuplicates= someArrayList.size();

int numOfDuplicates = originalSize = withoutDuplicates;
 
I quit.

Code:
int numOfDuplicates = originalSize - withoutDuplicates;
 
I see your point Mikey - although, your code returns the number of duplicates in an array (unless I am mistaken), when what I was trying to acheive was to retreive the number of times string "xyz" was duplicated in an array.

Since my original post, I have modified my code and now it is working as planned....

Code:
// ########## Check if any of the attendees ProposalResponses match ##########
		ArrayList<String> allResponses = new ArrayList<String>();
		
		// Add each attendees proposal responses to the allResponses list
		for (int a=0; a < attendees.length; a++) {
			String[] pr = memo.getPropertyAsList(attendees[a] + "ProposalResponses");
			if (!(pr.length == 0)) { // Ensure there ARE proposal responses from this attendee
				for (int r=0; r<pr.length; r++) {
					allResponses.add(pr[r]);
				}
			}
		}
		
		// Check if each attendee has responded with a matching date/time 
		int x = 0;
		while (x < allResponses.size()) { // While there are still elements in allResponses
			
			String compare = allResponses.get(0); // Store the first meeting response in list
			int matchFound = 0; // How many times has compare been found in allResponses
			
			// For all of the elements in allResponses...
			for (int i=0; i<allResponses.size(); i++) {
			    
				// The element is the same as the compare element
				if (allResponses.get(i).equals(compare)) {
			        matchFound++; // Add 1 to the number of matches found
			    }
			    
			    // If we have an agreed date/time between all attendees    
			    if (matchFound == attendees.length) {  
			    	// Update memo with the agreed meeting time for all attendees
					memo.setProperty("Bidding Outcome", "confirm");
					memo.setProperty("Confirm Meeting", compare);
					return memo;
			    }
			}
			
			// Remove the compared elements from the list
			for (int p=0; p<matchFound; p++) {
				allResponses.remove(compare);
			}
		}

A big thank you for all your help everybody!
 
So you're still searching for elements with a loop. Did you check the indexOf method?

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top