数组中所有重复次数大于等于minTimes的数字
- class Program
- {
- static void Main(string[] args)
- {
- int[] input = { 1, 1, 1, 2, 2, 5, 2, 4, 9, 9, 20 };
- IDuplicationFinder dup1 = new Duplication1();
- string str1 = dup1.FindDuplication(input, 3);
- Console.WriteLine(str1);
- IDuplicationFinder dup2 = new Duplication2();
- string str2 = dup2.FindDuplication(input, 3);
- Console.WriteLine(str2);
- Console.Read();
- }
- }
- class Duplication1 : IDuplicationFinder
- {
- public string FindDuplication(int[] input, uint minTimes)
- {
- Dictionary<int, int> valuedic = new Dictionary<int, int>();
- #region 整个完全遍历
- //foreach (var v in input)
- //{
- // var numtimes = input.Where(m => m == v).Count();
- // if (numtimes >= minTimes && !valuedic.Keys.Contains(v))
- // valuedic.Add(v, numtimes);
- //}
- #endregion
- #region linq group
- var groupnum = input.GroupBy(m => m);
- foreach (var g in groupnum)
- {
- if (g.Count() >= minTimes)
- valuedic.Add(g.Key, g.Count());
- }
- #endregion
- return GetValue(valuedic);
- }
- private static string GetValue(Dictionary<int, int> valuedic)
- {
- StringBuilder sb = new StringBuilder();
- foreach (var d in valuedic)
- {
- sb.AppendFormat("{0}出现了{1}次", d.Key, d.Value);
- sb.AppendLine();
- }
- return sb.ToString();
- }
- }
- class Duplication2 : IDuplicationFinder
- {
- public string FindDuplication(int[] input, uint minTimes)
- {
- List<int> result = new List<int>();
- Array.Sort(input);
- if (minTimes == 1)//如果次数是1,就要把去重显示
- {
- if (input[0] == input[1])
- result.Add(input[1]);
- for (int i = 1; i < input.Length - 1; i++)
- {
- if (input[i] != input[i + 1])
- result.Add(input[i + 1]);
- }
- }
- else
- {
- int count = 1;//排序后 前一个跟后一个对比,所以从1开始
- for (int i = 0; i < input.Length - 1; i++)
- {
- if (result.Count > 0 && result.Last() == input[i])
- continue;
- if (input[i] == input[i + 1])
- {
- count++;
- if (count >= minTimes)
- {
- result.Add(input[i]);
- count = 1;
- }
- }
- }
- }
- return string.Join(",", result);
- }
- }
- interface IDuplicationFinder
- {
- string FindDuplication(int[] input, uint minTimes);
- }
- t=[1,22,33,1,44,22,11,3,224,5,6,22,1,44]//查找出现次数最多的数字和次数 ruby
- hst={}
- t.each do |item|
- if(hst.key?(item))
- hst[item]+=1
- else
- hst[item]=1
- end
- end
- p hst
- b = Hash[hst.sort_by(){ |k, v| v }.reverse]
- p b
- p b.first
数组中所有重复次数大于等于minTimes的数字的更多相关文章
- 【C语言】统计数字在排序数组中出现的次数
//数字在排序数组中出现的次数. //统计一个数字在排序数组中出现的次数.比如:排序数组{1,2,3,3,3,3,4,5}和数字3,因为3出现了4次,因此输出4. #include <stdio ...
- JZ-037-数字在排序数组中出现的次数
数字在排序数组中出现的次数 题目描述 统计一个数字在升序数组中出现的次数. 题目链接: 数字在排序数组中出现的次数 代码 /** * 标题:数字在排序数组中出现的次数 * 题目描述 * 统计一个数字在 ...
- 剑指Offer面试题:32.数字在排序数组中出现的次数
一.题目:数字在排序数组中出现的次数 题目:统计一个数字在排序数组中出现的次数.例如输入排序数组{1,2,3,3,3,3,4,5}和数字3,由于3在这个数组中出现了4次,因此输出4. 二.解题思路 2 ...
- 剑指offer系列41---数字在数组中出现的次数
[题目]统计一个数字在排序数组中出现的次数. package com.exe9.offer; /** * [题目]统计一个数字在排序数组中出现的次数. * @author WGS * */ publi ...
- 剑指offer——python【第37题】数字在排序数组中出现的次数
题目描述 统计一个数字在排序数组中出现的次数 思路 最贱的方法依旧是count计数.. 当然,,看到有序数组就应该想到二分法,找到重复数字左边和右边的数字,然后两个相减就可以了 解答 方法1 coun ...
- 《剑指offer》第五十三题(数字在排序数组中出现的次数)
// 面试题53(一):数字在排序数组中出现的次数 // 题目:统计一个数字在排序数组中出现的次数.例如输入排序数组{1, 2, 3, 3, // 3, 3, 4, 5}和数字3,由于3在这个数组中出 ...
- 剑指offer37:统计一个数字在排序数组中出现的次数
1 题目描述 统计一个数字在排序数组中出现的次数. 2 思路和方法 (1)查找有序数组,首先考虑使用二分查找,使时间复杂度为O(log n).更改二分查找的条件,不断缩小区间,直到区间头和区间尾均为k ...
- Leetcode26——删除有序数组中的重复项(双指针法)
Leetcode26--删除有序数组中的重复项(双指针法) 1. 题目简述 给你一个升序排列的数组 nums ,请你原地 删除重复出现的元素,使每个元素只出现一次 ,返回删除后数组的新长度.元素的相对 ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
随机推荐
- ZOJ 1074 最大子矩阵和
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- 【BZOJ1030】文本生成器
Description JSOI交给队员ZYX一个任务,编制一个称之为“文本生成器”的电脑软件:该软件的使用者是一些低幼人群,他们现在使用的是GW文本生成器v6版.该软件可以随机生成一些文章―――总是 ...
- 前端跨域之html5 XMLHttpRequest Level2
前端代码 var xhr=new XMLHttpRequest(); xhr.open('POST','http://127.0.0.1:8081/ceshi',true); xhr.onreadys ...
- python学习笔记12(函数三): 参数类型、递归、lambda函数
一.函数参数的类型 之前我们接触到的那种函数参数定义和传递方式叫做位置参数,即参数是通过位置进行匹配的,从左到右,依次进行匹配,这个对参数的位置和个数都有严格的要求.而在Python中还有一种是通过参 ...
- 简单3d RPG游戏 之 005 选择敌人
选择一个敌人,按ctrl+d,复制出3个,调整一下它们的位置,不重叠,修改Tag为Enemy,禁用EnemyAI. 创建Targetting脚本,绑定到Player玩家对象 public class ...
- 【贪心】 BZOJ 3252:攻略
3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 261 Solved: 90[Submit][Status][Discuss] De ...
- [转载]再谈iframe自适应高度
Demo页面:主页面 iframe_a.html ,被包含页面 iframe_b.htm 和 iframe_c.html 下面开始讲: 通过Google搜索iframe 自适应高度,结果5W多条,搜索 ...
- WPF SplitButton 的杂七杂八
原文: http://www.codeproject.com/Articles/20612/A-WPF-SplitButton SplitButton.cs using System; using S ...
- FireFly 服务端 Unity3D黑暗世界 客户端 问题
启动服务端成功截图: 连接成功截图: 测试服务端是否启动成功: 在Web输入:http://localhost:11009/ 按回车 (查看cmd启动的服务端 是否多出如下显示) 服务端启动成功.P ...
- 学点PYTHON基础的东东--数据结构,算法,设计模式---访问者模式
说实话,感觉不是特别多,可能没遇到过多场面, 所以对应用场景没感觉吧. 反正,各种模式就是把类的实例传来传去,久而久之,产生了一些规律...:) # 轮子,引擎, 车身这些定义好了都不需要变动 cla ...