From 208c5d82c754881027a1753236400bfebbe0fd38 Mon Sep 17 00:00:00 2001 From: fartem Date: Fri, 9 May 2025 07:11:58 +0300 Subject: [PATCH] 2025-05-09 v. 1.1.1: added "3005. Count Elements With Maximum Frequency" --- README.md | 1 + ...count_elements_with_maximum_frequency.dart | 22 ++++++++++++++ pubspec.yaml | 2 +- ..._elements_with_maximum_frequency_test.dart | 30 +++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 lib/easy/3005_count_elements_with_maximum_frequency.dart create mode 100644 test/easy/3005_count_elements_with_maximum_frequency_test.dart diff --git a/README.md b/README.md index f6367ba..64e6d80 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 118. Pascal's Triangle | [Link](https://leetcode.com/problems/pascals-triangle/) | [Link](./lib/easy/118_pascals_triangle.dart) | | 263. Ugly Number | [Link](https://lettcode.com/problems/ugly-number/) | [Link](./lib/easy/263_ugly_number.dart) | | 500. Keyboard Row | [Link](https://leetcode.com/problems/keyboard-row/) | [Link](./lib/easy/500_keyboard_row.dart) | +| 3005. Count Elements With Maximum Frequency | [Link](https://leetcode.com/problems/count-elements-with-maximum-frequency/) | [Link](./lib/easy/3005_count_elements_with_maximum_frequency.dart) | | 3120. Count the Number of Special Characters I | [Link](https://leetcode.com/problems/count-the-number-of-special-characters-i/) | [Link](./lib/easy/3120_count_the_number_of_special_characters_i.dart) | | 3280. Convert Date to Binary | [Link](https://leetcode.com/problems/convert-date-to-binary/) | [Link](./lib/easy/3280_convert_date_to_binary.dart) | | 3340. Check Balanced String | [Link](https://leetcode.com/problems/check-balanced-string/) | [Link](./lib/easy/3340_check_balanced_string.dart) | diff --git a/lib/easy/3005_count_elements_with_maximum_frequency.dart b/lib/easy/3005_count_elements_with_maximum_frequency.dart new file mode 100644 index 0000000..fc788d1 --- /dev/null +++ b/lib/easy/3005_count_elements_with_maximum_frequency.dart @@ -0,0 +1,22 @@ +class Solution { + int maxFrequencyElements(List nums) { + final freq = {}; + var maxFreq = 0; + var result = 0; + + for (final num in nums) { + freq[num] = (freq[num] ?? 0) + 1; + + if (freq[num] > maxFreq) { + maxFreq = freq[num]; + result = 0; + } + + if (freq[num] == maxFreq) { + result += maxFreq; + } + } + + return result; + } +} diff --git a/pubspec.yaml b/pubspec.yaml index c03c4fe..b792de2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: leetcode_dart description: Some solved problems from https://leetcode.com on Dart -version: 1.1.0 +version: 1.1.1 homepage: https://github.com/fartem/leetcode-dart environment: diff --git a/test/easy/3005_count_elements_with_maximum_frequency_test.dart b/test/easy/3005_count_elements_with_maximum_frequency_test.dart new file mode 100644 index 0000000..31b5680 --- /dev/null +++ b/test/easy/3005_count_elements_with_maximum_frequency_test.dart @@ -0,0 +1,30 @@ +import 'package:leetcode_dart/easy/3005_count_elements_with_maximum_frequency.dart'; +import 'package:test/test.dart'; + +void main() { + group( + 'Example tests', + () { + final solution = Solution(); + + test( + 'Test case 1', + () => expect( + 4, + solution.maxFrequencyElements( + [1, 2, 2, 3, 1, 4], + ), + ), + ); + test( + 'Test case 2', + () => expect( + 5, + solution.maxFrequencyElements( + [1, 2, 3, 4, 5], + ), + ), + ); + }, + ); +}