Skip to content

Commit 38fae4e

Browse files
committed
1207. Unique Number of Occurrences
1 parent 534829c commit 38fae4e

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
| 1129 | Shortest Path with Alternating Colors | [Ruby](./algorithms/ruby/1129-shortest-path-with-alternating-colors.rb) | Medium |
152152
| 1155 | Number of Dice Rolls With Target Sum | [Ruby](./algorithms/ruby/1155-number-of-dice-rolls-with-target-sum.rb) | Medium |
153153
| 1162 | As Far from Land as Possible | [Ruby](./algorithms/ruby/1162-as-far-from-land-as-possible.rb) | Medium |
154+
| 1207 | Unique Number of Occurrences | [Ruby](./algorithms/ruby/1207-unique-number-of-occurrences.rb) | Easy |
154155
| 1254 | Number of Closed Islands | [Ruby](./algorithms/ruby/1254-number-of-closed-islands.rb) | Medium |
155156
| 1319 | Number of Operations to Make Network Connected | [Ruby](./algorithms/ruby/1319-number-of-operations-to-make-network-connected.rb) | Medium |
156157
| 1372 | Longest ZigZag Path in a Binary Tree | [Ruby](./algorithms/ruby/1372-longest-zigzag-path-in-a-binary-tree.rb) | Medium |
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
# 1207. Unique Number of Occurrences
4+
# https://leetcode.com/problems/unique-number-of-occurrences
5+
# Easy
6+
7+
=begin
8+
Given an array of integers arr, return true if the number of occurrences of each value in the array is unique or false otherwise.
9+
10+
Example 1:
11+
Input: arr = [1,2,2,1,1,3]
12+
Output: true
13+
Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.
14+
15+
Example 2:
16+
Input: arr = [1,2]
17+
Output: false
18+
19+
Example 3:
20+
Input: arr = [-3,0,1,-3,1,1,1,-3,10,0]
21+
Output: true
22+
23+
Constraints:
24+
1 <= arr.length <= 1000
25+
-1000 <= arr[i] <= 1000
26+
=end
27+
28+
# @param {Integer[]} arr
29+
# @return {Boolean}
30+
def unique_occurrences(arr)
31+
!arr.tally.values.uniq!
32+
end
33+
34+
# **************** #
35+
# TEST #
36+
# **************** #
37+
38+
require "test/unit"
39+
class Test_unique_occurrences < Test::Unit::TestCase
40+
def test_
41+
assert_equal true, unique_occurrences([1, 2, 2, 1, 1, 3])
42+
assert_equal false, unique_occurrences([1, 2])
43+
assert_equal true, unique_occurrences([-3, 0, 1, -3, 1, 1, 1, -3, 10, 0])
44+
end
45+
end

0 commit comments

Comments
 (0)