/* * @lc app=leetcode id=1207 lang=rust * * [1207] Unique Number of Occurrences * * https://leetcode.com/problems/unique-number-of-occurrences/description/ * * algorithms * Easy (71.22%) * Total Accepted: 37.1K * Total Submissions: 52K * Testcase Example: '[1,2,2,1,1,3]' * * Given an array of integers arr, write a function that returns true if and * only if the number of occurrences of each value in the array is unique. * * * Example 1: * * * 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. * * Example 2: * * * Input: arr = [1,2] * Output: false * * * Example 3: * * * Input: arr = [-3,0,1,-3,1,1,1,-3,10,0] * Output: true * * * * Constraints: * * * 1 <= arr.length <= 1000 * -1000 <= arr[i] <= 1000 * * */ impl Solution { pub fn unique_occurrences(arr: Vec) -> bool { let eye = vec_counter(arr); let mut x:HashSet = HashSet::new(); for k in eye.keys(){ x.insert(eye[k]); } eye.len() == x.len() } } // pub structSolution; use std::collections::HashMap; use std::collections::HashSet; use std::fmt::Debug; use std::hash::Hash; #[allow(dead_code)] pub fn vec_counter(arr: Vec) -> HashMap { let mut c = HashMap::new(); for n in arr { *c.entry(n).or_insert(0) += 1; } c } #[allow(dead_code)] pub fn print_map(map: &HashMap) { for (k, v) in map.iter() { println!("{:?}: {:?}", k, v); } } #[allow(dead_code)] pub fn say_vec(nums: Vec){ println!("{:?}", nums); } #[allow(dead_code)] pub fn char_frequency(s: String) -> HashMap { let mut res:HashMap = HashMap::new(); for c in s.chars(){ *res.entry(c).or_insert(0) += 1; } res }