tags:

views:

41

answers:

3

I have a maybe simple problem. I am going to generate 3 random numbers ranging from 0 to 2 and I want to determine if there are any duplicates.

Any ideas?

+2  A: 
if (num1 == num2) {
}
else if (num1 == num3) {
}
else if (num2 == num3) {
}
else {
     //There are no dups.
}

Checks if there is a duplicate.

if (num1 == num2) {
     counter++;
}
if (num1 == num3) {
     counter++;
}
if (num2 == num3) {
     counter++;
}

This finds how many duplicates there are (for an added bonus).

EDIT:

For x amount of numbers you might want to do this (using 10 as my example amount of ints):

int counter = 0;
int i[10] = {
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10
};

for (int g = 0; g < 10; g++)
{
    for (int j = g+1; j < 10; j++)
    {
        if(i[g] == i[j])
        {
            counter++;
            printf(@"%d\n", counter);
            //If this if statement is true then there is a dup... In this case none are found.
        }
    }
}
thyrgle
What happens if you want to test for 10 random numbers? It's going to get very long
Brock Woolf
Brock Woolf: True. Updated answer. :)
thyrgle
The edits not necessary I only need to test 3 random numbers. But thanks for the effort!
A: 

How about this?

NSArray *randomNumbers  = [[NSArray alloc] initWithObjects:@"0",@"1",@"1",@"2",nil];    
NSMutableDictionary *occurenceDict = [[NSMutableDictionary alloc] init];

for (NSString *number in randomNumbers) 
{
    if ([occurenceDict objectForKey:number] == nil) {
        [occurenceDict setValue:[NSNumber numberWithInt:[number intValue]] forKey:number];
        int occOfNum = 0;
        for (int i = 0; i < [randomNumbers count]; i++) {
            NSString *currentNumber = [randomNumbers objectAtIndex:i];
            if ([currentNumber compare:number] == NSOrderedSame) {
                occOfNum++;
            }
        }
        [occurenceDict setValue:[NSNumber numberWithInt:occOfNum] forKey:number];
    }
}

for (NSString *key in occurenceDict) {
    NSString *occurrences = [occurenceDict objectForKey:key];
    NSLog(@"Number %d is contained %d times", [key intValue], [occurrences intValue]);
}

[randomNumbers release];
[occurenceDict release];

Output:

Number 0 is contained 1 times
Number 1 is contained 2 times
Number 2 is contained 1 times

Edit: Incase you want to know how this works, here is the same version but with comments to help you understand it:

// Create array with the numbers that we have randomly generated
NSArray *randomNumbers  = [[NSArray alloc] initWithObjects:@"0",@"1",@"1",@"2",nil];    
NSMutableDictionary *occurenceDict = [[NSMutableDictionary alloc] init];

for (NSString *number in randomNumbers) 
{
    // If this number has not been added to the dictionary
    if ([occurenceDict objectForKey:number] == nil) {
        // Add it
        [occurenceDict setValue:[NSNumber numberWithInt:[number intValue]] forKey:number];
        // Find how many times the number occurs with the "randomNumbers" array
        int occOfNum = 0;
        for (int i = 0; i < [randomNumbers count]; i++) {
            NSString *currentNumber = [randomNumbers objectAtIndex:i];
            if ([currentNumber compare:number] == NSOrderedSame) {
                // We found this number at this index, so increment the found count
                occOfNum++;
            }
        }
        // Save the number of times which "number" occurs in the dictionary for later
        [occurenceDict setValue:[NSNumber numberWithInt:occOfNum] forKey:number];
    }
}

// Iterate through all items in the dictionary and print out the result
for (NSString *key in occurenceDict) {
    NSString *occurrences = [occurenceDict objectForKey:key];
    NSLog(@"Number %d is contained %d", [key intValue], [occurrences intValue]);
}
// Release alloc'ed memory
[randomNumbers release];
[occurenceDict release];
Brock Woolf
A: 

Crikey, these answers are long-winded! Put your random generated numbers into an NSIndexSet. Test the set before inserting a number and you'll know that the number is already present, and so is a dupe.

Mike Abdullah