How can I check if 3 out of 5 integers are the same

Say I have 5 integers.

int a = 1;
int b = 2;
int c = 5;
int d = 1;
int f = 1;

I want to check if one of these three out of 5 integers is the same.

I tried some things, but it is very long (over 500 lines) and thought it was not a good method.

+3
source share
5 answers

first put them all in one collection, but do not have separate variables:

var numbers = new[]{a,b,c,d,f};

Then group them, find the counter of each group and see if any of your criteria match.

var isLargeGroup = numbers.GroupBy(n => n, (key, group) => group.Count() )
    .Any(count => count >= 3);
+8
source

(, 10000 ), , . , :

private bool AreNumbersTheSame(int[] numbers, int duplicateCount)
{
    var numberCounts = new Dictionary<int, int>();
    for (int i = 0; i < numbers.Length; i++)
    {
        var current = numbers[i];
        if (!numberCounts.ContainsKey(current))
        {
            numberCounts[current] = 1;
            continue;
        }

        if (numberCounts[current] == duplicateCount - 1)
        {
            return true;
        }

        numberCounts[current]++;
    }
    return false;
}

:

var result = AreNumbersTheSame(new[] { a, b, c, d, f }, 3);
+1

, , , GroupBy, , . , .

public static bool HasDuplicateCount<T>(this IEnumerable<T> seq, int maxCount)
{
    Dictionary<T, int> counts = new Dictionary<T, int>();
    foreach (T t in seq)
    {
        int count;
        if (counts.TryGetValue(t, out count))
            ++count;
        else
            count = 1;
        if (count == maxCount)
            return true;
        counts[t] = count;
    }
    return false;
}

:

bool threeDups = new[] { a, b, c, d, f }.HasDuplicateCount(3);
0

Majority Vote, , . ( 3 5 ):

public static bool HasMajorityElement(int[] a)
{
    int candidate = 0;
    int count = 0;
    foreach (int i in a)
    {
        if (count == 0)
        { 
            candidate = i;
            count = 1;
        }
        else 
        {
            count += candidate == i ? 1 : -1;
        }
    }
    count = 0;
    foreach (int i in a)
    {
        if (i == candidate) ++count;
    }
    return count > a.Length / 2;
}

ideone.com.

, , :

public static bool ThreeOutOfFive(int a, int b, int c, int d, int e)
{
    return ((a==b?1:0)+(a==c?1:0)+(a==d?1:0)+(a==e?1:0) > 1)
        || ((b==c?1:0)+(b==d?1:0)+(b==e?1:0) > 1)
        || ((c==d?1:0)+(c==e?1:0) > 1);
}
0

: -

  • .

  • KEY VALUE.

  • When repeating the list, you need to check if the number is in the dictionary, and then add it using the number as the key and 1 as the VALUE value.

  • If the number repeats, just increase the VALUE dictionary by 1.

  • Once it is 3, break out of the loop.

            int a = 1;
            int b = 2;
            int c = 5;
            int d = 1;
            int f = 1;
            var listOfNumbers = new List<int> {a, b, c, d, f};
    
            var dict = new Dictionary<int, int>();
            foreach (var number in listOfNumbers)
            {
                if (dict.ContainsKey(number))
                {
                    dict[number] = dict[number] + 1; //if a key repeats => increment the value by 1
                    if (dict[number] == 3)
                        break; //found the number
                }
                else
                    dict.Add(number, 1); //for first occurence of the key => initialize the value with 1 
            }
    
0
source

All Articles