/* * @lc app=leetcode id=1287 lang=rust * * [1287] Element Appearing More Than 25% In Sorted Array * * https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/description/ * * algorithms * Easy (60.00%) * Total Accepted: 12.6K * Total Submissions: 21.1K * Testcase Example: '[1,2,2,6,6,6,6,7,10]' * * Given an integer array sorted in non-decreasing order, there is exactly one * integer in the array that occurs more than 25% of the time. * * Return that integer. * * * Example 1: * Input: arr = [1,2,2,6,6,6,6,7,10] * Output: 6 * * * Constraints: * * * 1 <= arr.length <= 10^4 * 0 <= arr[i] <= 10^5 * */ impl Solution { pub fn find_special_integer(arr: Vec) -> i32 { let k = arr.len() / 4; *arr.iter().zip(arr.iter().skip(k)).filter(|(x, y)| x == y).next().unwrap().0 } } // pub structSolution; use std::collections::HashMap; use std::collections::HashSet; use std::fmt::Debug; use std::hash::Hash; use std::iter::FromIterator; // use std::collections::VecDeque; // use std::collections::BTreeMap; #[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 } #[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 vec_to_hashset(arr: Vec) -> HashSet { HashSet::from_iter(arr.iter().cloned()) } #[allow(dead_code)] pub fn int_to_char(n: i32) -> char { // Convert number 0 to a, 1 to b, ... assert!(n >= 0 && n <= 25); (n as u8 + 'a' as u8) as char }