2

What I would like to do is have one array compare itself to another array and if it finds a duplicate in the comparing array it will skip the following statement, here is what I've got at the moment

for (int count=0; count < userId.length; count++) {
    for (int integer=0; userId[count] != excluded[integer]; integer++) {
        // The arrays hold either string or Long variables in them
        // Continued code to finish off after checking array
    }
}

what I want to do is get this to work, however possible but keep it as simple as possible at the same time. incase the code is actually not clear at all I want to compare two arrays userId and excluded, and what I want is that if any of the userId values in the array match any in excluded then like the array states I want to exclude them from the list.

EDIT: when running if (excluded[counts].contains(user))
I get the specific output I wanted "GREAT!"
but the problem I have now is if I run the if as if (!excluded[counts].contains(user))<br> I get the excluded values and then some, many of the values repeat minus those that show
Example:

String[] userId = new String [] { "10", "15", "17", "20", "25", "33", "45", "55", "77" }
String[] excluded = new String[] { "15", 20", "55", "77" }

then I get to my loop to check the arrays

int count=0;
for (String user : userId) {
for (int counts=0; counts < excluded.length; counts++) {
if (!excluded[counts].contains(user)) {
System.out.println("UID = " + userID[count]);
}
count++

that !excluded will still show instances of the userId that I don't want to be shown, so it still shows "UID = 15" even though I want it to exclude it, which it does, but only once so instead of seeing it 4 times I see it 3 times.

2
  • Are you saying you have to use this initial code setup to solve your problem or are you open to suggestions of better ways of doing it? Commented Sep 19, 2012 at 20:04
  • 1
    do you mean: for each element in userId but not in excluded? Commented Sep 19, 2012 at 20:04

3 Answers 3

3

Just do this using the collections framework. Assuming your IDs are int values:

int[] excluded = ... ;
int[] userIds = ... ;
Set<Integer> excludedIds = new HashSet<Integer>(Arrays.asList(excluded));
for (int userId : userIds) {
    if (excluded.contains(userId))
        continue;
    // Do something with ID
}

You could also do this, but this assumes that you don't care about duplicates in the userIds array:

int[] excluded = ... ;
int[] userIds = ... ;
Set<Integer> excludedIds = new HashSet<Integer>(Arrays.asList(excluded));
Set<Integer> userIdSet = new HashSet<Integer>(Arrays.asList(userIds));
userIdSet.removeAll(excludedIds);
for (int userId : userIdSet) {
    // Do something with ID
}

This is better optimized, but not particularly necessary unless you have lots of IDs. Your algorithm is O(n) = n2, mine here is just O(n) = n.

Sign up to request clarification or add additional context in comments.

5 Comments

The Set solution of course assuming that userIds does not contain duplicates (it may be a record of e.g. users entering a chatroom...)
they aren't int arrays their all String or Long
@FateAverie Then simply replace Set<Integer> with Set<Long> or Set<String> and it will work without any extra logic.
after running the if (excluded.contains(userId)) I check the remaining values that it skips over and there are multiple passes where the userId doesn't get picked up and passes through since the value in the array doesn't match the current value, but it did match it in the previous pass. a println reveals the the counts check each time e.g. count=1 counts= 0, 1, 2, 3, 4 where it picks up 2 but drops all the others which are the exact same thing
Could you clarify what your comment means a little more in your question? Maybe with some code?
2

Put the excluded IDs into a set.

Set<Integer> excludedSet = new HashSet<Integer>();
for (int i : excluded) {
  excludedSet.add(i);
}

Then your loop looks like this:

for (int id : userId) {
    if (!excludedSet.contains(id)) {
        // process this user
    }
} 

Comments

1

I wouldn't bother with optimizations unless you need to do this task more than once.

Otherwise the best solution is to put items of second array into Set and use it's efficient methods (contains) to resolve if the collection has items.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.