数组中所有重复次数大于等于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 ...
随机推荐
- Java内存区域与内存溢出异常(二)
了解Java虚拟机的运行时数据区之后,大致知道了虚拟机内存的概况,内存中都放了些什么,接下来将了解内存中数据的其他细节,如何创建.如何布局.如何访问.这里虚拟机以HotSpot为例,内存区域以Java ...
- C# Windows - RadioButton&CheckBox
RadioButton和CheckBox控件与Button控件有相同的基类,但它们的外观和用法大不相同. RadioButton显示为一个标签,左边是一个圆点,该点可以是选中或未选中.用在给用户提供两 ...
- sql之透视
1.透视原理:就是将查询结果进行转置 下面就举例来说明: 执行下面语句:检查是否含有表 dbo.Orders,如果有就将表删除: if OBJECT_ID('dbo.Orders','U') is n ...
- iOS 的 Gif 渲染引擎 FLAnimatedImage-b
公司的项目有个首页加载一张2M左右的git图,刚做的时候是使用的SDWebImage里面的方法: + (UIImage *)sd_animatedGIFNamed:(NSString *)name; ...
- 记一次apt-get无法安装git的问题
解决apt-get安装过程中出现的Size mismatch和Hash Sum mismatch问题. 事情起因 我从单位复制了一个Virtualbox虚拟机(ubuntu 15.04 Desktop ...
- Otto Product Classification Winner's Interview: 2nd place, Alexander Guschin ¯\_(ツ)_/¯
Otto Product Classification Winner's Interview: 2nd place, Alexander Guschin ¯\_(ツ)_/¯ The Otto Grou ...
- Bypass Preventing CSRF
CSRF在过去的n年(n>2)一直都火,在bh/defcon/owasp等会议上多次探讨CSRF的攻防[具体你可以看看以往的那些pp].前 段时间PLAYHACK.net上发表了一个总结性的pp ...
- Unity3D的几种坐标系
原地址:http://www.cnblogs.com/martianzone/p/3371789.html http://www.cnblogs.com/88999660/archive/2013/0 ...
- Interface Serializable
public interface Serializable Serializability of a class is enabled by the class implementing the ja ...
- ***SQL统计语句总结(运用场景:运营分析,财务分析等)
-- 统计三月的每天的数据量 ,) ,) ; --统计从5月19到6月29的数据量 , ) AS '日期', count(*) AS '医说数' FROM xm_feed a WHERE a.feed ...