Created
January 29, 2024 02:07
-
-
Save unilecs/bd4ae0fc5973b70e3e77e0fcdb3484b7 to your computer and use it in GitHub Desktop.
Задача: Часто встречаемый элемент *
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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