Check times a value is next to the same value in an array

Lets say I have this array on C#:

int myList = {1,4,6,8,3,3,3,3,8,9,0}

I want to know if a value (lets say from 0-9) is next to itself in the list and how many times. In this case, the value 3 is next to itself and it has 4 repetitions. If I have a list {0,1,2,3,4,5,5,6,7} the value 5 is next to itself and has 2 repetitions.

Repetitions have a limit of 5. No value can be repeated more than 5 times. The far I went is making if statements, but I know there’s a better way of doing it.

You could use a function like this:

public int GetRepetitions(int[] myList, int value)
{
    int repetitions = 0;
    for (int i = 0; i < myList.Length; i++)
    {
        if ((myList *== value) && ((i == 0 ? false : myList[i - 1] == value) ||*

(i == myList.Length - 1 ? false : myList[i+1] == value)))
{
repetitions++;
}
}
return repetitions;
}
In case you haven’t seen it, the a ? b : c operator just means if a then b else c.
Edit: hehe, too slow, either will work.

basic forloop will do

using System;

void Start(){
    int[] myArray = new int[]{1,1,1,3,3,3,3};
    Debug.Log(howManyRepetitionsAfterFirstOccurance(ref myArray, 3) );
}

int howManyRepetitionsAfterFirstOccurance(ref int[] array2Inspect, int value2Check){ //pass array by reference (don't copy it over, just point your finger at the one to work with
   startingPos = Array.IndexOf(array2Inspect, value2Check) //get the index of the first occurance of the number you need
   int repetitions = 0;

      for(int i = startingPos; i < array2Inspect.Length; i++){  //see if its present once or more in the array
        int value_at_cell = array2Inspect[ i ];      

        if (startingPos == -1 ||  value_at_cell != value2Check) break;

        repetitions++;
   }

   return  repetitions;
}

Already answered, but i’ll just leave this here for fun :slight_smile:

void countNumber(int[] array, int value, out int longest, out int cumulative, out int first, out int last)
{
	cumulative = longest = first = last = 0;

	int lastIndex = array.Length - 1;
	int currentStreak = 1;
	bool wasMatch = false;

	for(int i = 0; i <= lastIndex; i++)
	{  
		bool isMatch = array *== value;*
  •  if (wasMatch)*
    
  •  {*
    
  •  	if (isMatch)*
    
  •  	{*
    
  •  		currentStreak++;*
    
  •  	}*
    
  •  	bool endOfStreak = !isMatch || i == lastIndex;*
    
  •  	if (endOfStreak && currentStreak > 1)*
    
  •  	{*
    
  •  		if (currentStreak > longest) longest = currentStreak;*
    
  •  		if (first == 0) first = currentStreak;*
    
  •  		last = currentStreak;*
    
  •  		cumulative += currentStreak;*
    
  •  		currentStreak = 1;*
    
  •  	}*
    
  •  }*
    
  •  wasMatch = isMatch;*
    
  • }*
    }

I have this solution which can pick the consecutive repeated elements (even if they are repeated itself too).

e.g., cases like:

{ 1, 4, 3, 6, 6, 8, 3, 3, 3, 3, 8, 9,
0 }; // Here 6 and 3 are repeated

{ 1, 4, 3, 3, 6, 6, 8, 3, 3, 3, 3, 8,
9, 0 }; // Here 6 repeated once and 3
repeated twice

Sample Code:

int[] myList = new int[] { 1, 4, 3, 3, 6, 6, 8, 3, 3, 3, 3, 8, 9, 0 };
Dictionary<int, List<int>> groupDict = new Dictionary<int, List<int>>();
string characters = string.Join("", myList.ToArray());

for (int i = 0; i < characters.Length; i++)
{
    int cInt = int.Parse(characters*.ToString());*

MatchCollection mColl = Regex.Matches(characters.Substring(i), “^” + characters + “{2,}”);
if (mColl.Count > 0)
{
if (!groupDict.Keys.Contains(cInt))
{
groupDict.Add(cInt, new List());
}

groupDict[cInt].Add(mColl[0].Length);
i += mColl[0].Length - 1;
}
}
}
For first case it will show you: (in-memory result)
6 → 2
3 → 4
For second case it will show you: (in-memory result)
6 → 2
3 → 2, 4
Hope it is useful for you.