2

this is a very specific question, but i also have very specific details on what i'm looking for. i currently do not have (and cannot find) a good method for accomplishing this. please help if you can.

i have an integer list that will always contain 4 items and 3 of the items will always end in the same digit. i need to some how extract the 1 item that has a unique final digit. the unique item will not always be in the same location in the list and all numbers in the list will be a value from 0-40 (so one to two digits).

example list contents: 12,22,27,32. i need a method to return or extract the 27.

example 2: 4,13,23,33. i would need to return 4.

the solution should either remove the 3 repeated final digit numbers from the list or possibly create just a standard int variable with the unique value.

i've tried converting to a string and gather that character and have this ridiculous function that tests the integer length (number of digits) and adds just the end digit to another list and some comparison code. it's just really ridiculous. if you know of any ideas i should try, please let me know, thanks in advance.

ikathegreat
  • 2,311
  • 9
  • 49
  • 80

5 Answers5

4

Assuming numbers is some iterable of integers:

int unique = numbers.GroupBy(i => i % 10).Single(g => g.Count() == 1).Single();

This groups the numbers by their last digit, pulls out the only group with a single member, and returns its only member.

tzaman
  • 46,925
  • 11
  • 90
  • 115
  • thanks, that did the trick. i ended up adding the last digits to an array, using your code to find the unique value, use another for loop to compare that unique value to last digit of the original array (modulus), and compare. if the same, the rest of my program runs as expected. thanks again. – ikathegreat Jan 19 '12 at 02:44
1

number % 10 will give you the last digit of number.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
0

I am pretty sure there is a remainder calculation you can use in C# to solve this. In C it is %. Eg: 15%10 gives 5.

Now, let us assume we have four remainders: a,b,c & d.

If a==b:
    if c==b:
        return d
    else:
        return c
else:
    if a==c:
        return b
    else:
        return a

If you have lot of numbers instead of just 4:

def func(x):
    if len(x)<3:
       return NULL
    if x[0]!=x[1]:
        if x[0]==x[2]:
            return x[1]
        else:
            return x[0]
    for i in 2:(len(x)-1) :
        if x[0]!=x[i]:
            return x[i]
    return NULL
ElKamina
  • 7,747
  • 28
  • 43
  • oh yes we're getting warmer. should put the second part of the question somewhere else here or do you have any suggestions on how to remove that unique value from the original int list? i can possibly add the final digits to a new int list, find out the index of the unique digit and return that from the original list, or delete the duplicate digit values from their respective indices from the original list? sorry i'm just trying to figure out the best way to accomplish this, not just A way. thanks. – ikathegreat Jan 19 '12 at 02:02
0

for such a specific format, just use a simple if-then-else network on the remainder mod 10 of the numbers.

if(arem10==brem10) { return( (arem10==crem10) ? d : c); }
if(arem10==crem10) { return(b); }
return(a);
Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
ddyer
  • 1,792
  • 19
  • 26
  • sorry this syntax is a little hard to follow for me. say i have the modulus values already in a list, so for: 12,22,27,32 i have a list that contains: 2,2,7,2. how do i only get the 2? (again i can't simply ask for that index, it won't always be in that position) – ikathegreat Jan 19 '12 at 02:03
0

Some simple ifs here, although you are probably looking for fancy LINQ* :)

*LINQ is actually pretty fancy

//compare first to second
if((list[0] - list[1]) % 10 != 0)
{
    //they're not the same, see if first and third are different
    if((list[0] - list[2])% 10 != 0)
    {
        return list[0]; //yes, so first is the odd one
    }
    else
    {
        return list[1]; //no, so second is the odd one
    }
}

//first two are same, so check if third is the odd one
if((list[0] - list[2]) % 10 != 0)
    return list[2]; //yes it is

//only fourth remains
return list[3];
neeKo
  • 4,280
  • 23
  • 31