x elements in array equal

What would be a good way to write a function to check if any 3 elements in an array of length 5 are equal?
[1,2,1,4,1] = true
[1,1,1,2,3] = true
[1,2,1,4,2] = false

So here’s a Linq based solution:

``````  var maxMatching = someArray.GroupBy(c=>c).Max(c=>c.Count());
``````

This will return the maximum number of matches of any key in the array.

Now if you want to get an array of the matches:

``````  var matches = someArray.GroupBy(c=>c).Select(c=> new { Key = c.Key, Count = c.Count() }).OrderBy(c=>c.Count).ToArray();
``````

This will give you the matches in descending order of number in an array where each element in the array has a .Key member with the value that has that match and a .Count member with the number of matches.

#pragma strict
var array : int;
function Start () {
array = new int[5];
// seting random values to testing results.
for(var i=0;i<array.Length;i++)
array = Random.Range(0,5);
}
function Update () {
if(Input.GetKeyDown(“e”))
{
// testing to see if the result is coerent
Debug.Log(arrayhave3equals(array));
var arrstring : String=“[”;
for(var i=0;i<array.Length;i++)
arrstring +=array*+“,”;*
arrstring += “]”;
Debug.Log(arrstring);
}
}
function arrayhave3equals ( array : int[])
{
// which loop is the number of elements which need to be equals
for(var x=0;x<array.Length;x++)
{
* for(var y=0;y<array.Length;y++)*
* {*
* for(var z=0;z<array.Length;z++)*
* {*
* if(x!=y && x!=z && y!=z)*
* {*
* if((array[x] == array[y]) &&(array[x] == array[z]) && (array[z] == array[y]))*
* return(true);*
* }*

* }*
* }*
}
return(false);
}
this javascript will solve your problem.
each loop will look at all elements of the array searching if tree somes are equal, four loops will give true if four elements is equals. this will works with any kind of 1d array