Skip to content

Instantly share code, notes, and snippets.

@unilecs
Created January 29, 2024 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unilecs/bd4ae0fc5973b70e3e77e0fcdb3484b7 to your computer and use it in GitHub Desktop.
Save unilecs/bd4ae0fc5973b70e3e77e0fcdb3484b7 to your computer and use it in GitHub Desktop.
Задача: Часто встречаемый элемент *
using System;
public class Program
{
public static int FindFreqNumber(int[] arr)
{
int len = arr.Length;
if (len < 3)
{
return arr[0];
}
int size = len / 4;
for (int i = 0; i < len - size; i++)
{
if (arr[i] == arr[i + size])
{
return arr[i];
}
}
return -1;
}
public static void Main()
{
Console.WriteLine("UniLecs");
// tests
Console.WriteLine(FindFreqNumber(new int[] { 1, 3, 3, 7, 7, 7, 7, 8, 10 })); // 7
Console.WriteLine(FindFreqNumber(new int[] { 1, 2 })); // 1 или 2
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment