D
D
DeadDeveloper2019-09-11 00:24:14
C++ / C#
DeadDeveloper, 2019-09-11 00:24:14

Iterating over an array for matches!?

In general details, I have an array with 5 elements. (but in the future the number of elements can be increased, so we can assume that the array is not limited). And I need to comb the array for a match and indicate if there are matches, how many of them for each DIFFERENT element of the array. That is, (see below in the code), 3 matches in the first case, 3 matches in the second, with 2 matches for ones and 1 match for twos and 1 match in the third case. I understand that I need an algorithm like:

public int[] ScoreCombo;
    private void Start()
    {
        ScoreCombo = new int[5]{1, 2, 1, 1, 1};//Первый случай
    }
//Или к примеру {1, 1, 2, 2, 1} Второй случай
//Или {4, 2, 1, 5, 1} И третий случай
//И любые другие вариации
            int BufferMas = 0;
            for (int a = 1; a < ScoreCombo.Length; a++)
            {
                if(ScoreCombo[BufferMas] == ScoreCombo[a])//Тут я проверяю соответствия в массиве
                {
                    Debug.Log("Совпадение: " + "Element " + BufferMas + ", Element " + a);
                }
                if (a == 4 && ScoreCombo[BufferMas] != ScoreCombo[BufferMas + 1])//А здесь я проверяю, 
//Стоит ли делать второй проход со следующем индексом массива для поиска соответствий.
//Тут как по мне и кроется загвоздка в решении моей проблемы.
                {
                    BufferMas++;
                    a = BufferMas;
                }
            }

I hope I described enough, if necessary, I can completely throw off the code and the task itself with which the difficulty arose.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Vlasov, 2019-09-11
@andreyvlv

If I understand the question correctly, then through the dictionary it is done like this:

using System;
using System.Collections.Generic;

namespace GetRepetitions
{
    class Program
    {
        static void Main(string[] args)
        {
            var scoreCombo = new int[] { 1, 2, 1, 1, 1 };
            var repetitions = GetRepetitionsCount(scoreCombo);   
            
            foreach (var kv in repetitions)           
                Console.WriteLine($"Num: {kv.Key}, Rep. Count: {kv.Value}");            
        }

        static Dictionary<int, int> GetRepetitionsCount(int[] arr)
        {
            var repetitions = new Dictionary<int, int>();
            foreach (var num in arr)
                if (repetitions.ContainsKey(num))
                    repetitions[num]++;
                else
                    repetitions.Add(num, 0);
            return repetitions;
        }
    }
}

A
Alexander Pavlov, 2019-09-11
@Nokman

Using LINQ, you can group the elements of an array by their value and select those with more than 1.

using System;
using System.Linq;

public class Test
{
  public static void Main()
  {
        int[] ScoreCombo = new int[5] { 1, 2, 1, 1, 1 };
        foreach(var repeat in ScoreCombo.GroupBy(i => i).Where(g => g.Count() > 1))
            Console.WriteLine("Repeat: " + repeat.Key + " (" + repeat.Count() + ")");
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question