247просмотров
7.3%от подписчиков
23 марта 2026 г.
statsScore: 272
Задача: 1207. Unique Number of Occurrences
Сложность: easy Дан массив целых чисел arr. Верните true, если количество вхождений каждого значения в массиве уникально, или false в противном случае. Пример:
Input: arr = [1,2,2,1,1,3]
Output: true
Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences. 👨💻 Алгоритм: 1⃣Сохраните частоты элементов массива arr в хэш-таблице freq. 2⃣Итерируйтесь по хэш-таблице freq и вставьте частоты всех уникальных элементов массива arr в хэш-набор freqSet. 3⃣Верните true, если размер хэш-набора freqSet равен размеру хэш-таблицы freq, иначе верните false. 😎 Решение:
using System;
using System.Collections.Generic; public class Solution { public bool UniqueOccurrences(int[] arr) { Dictionary<int, int> freq = new Dictionary<int, int>(); foreach (int num in arr) { if (freq.ContainsKey(num)) { freq[num]++; } else { freq[num] = 1; } } HashSet<int> freqSet = new HashSet<int>(freq.Values); return freq.Count == freqSet.Count; }
} Ставь 👍 и забирай 📚 Базу знаний