1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. int[] input = { 1, 1, 1, 2, 2, 5, 2, 4, 9, 9, 20 };
  6. IDuplicationFinder dup1 = new Duplication1();
  7. string str1 = dup1.FindDuplication(input, 3);
  8. Console.WriteLine(str1);
  9.  
  10. IDuplicationFinder dup2 = new Duplication2();
  11. string str2 = dup2.FindDuplication(input, 3);
  12. Console.WriteLine(str2);
  13. Console.Read();
  14. }
  15. }
  16. class Duplication1 : IDuplicationFinder
  17. {
  18. public string FindDuplication(int[] input, uint minTimes)
  19. {
  20. Dictionary<int, int> valuedic = new Dictionary<int, int>();
  21. #region 整个完全遍历
  22. //foreach (var v in input)
  23. //{
  24. // var numtimes = input.Where(m => m == v).Count();
  25. // if (numtimes >= minTimes && !valuedic.Keys.Contains(v))
  26. // valuedic.Add(v, numtimes);
  27. //}
  28. #endregion
  29. #region linq group
  30. var groupnum = input.GroupBy(m => m);
  31. foreach (var g in groupnum)
  32. {
  33. if (g.Count() >= minTimes)
  34. valuedic.Add(g.Key, g.Count());
  35. }
  36. #endregion
  37. return GetValue(valuedic);
  38. }
  39. private static string GetValue(Dictionary<int, int> valuedic)
  40. {
  41. StringBuilder sb = new StringBuilder();
  42. foreach (var d in valuedic)
  43. {
  44. sb.AppendFormat("{0}出现了{1}次", d.Key, d.Value);
  45. sb.AppendLine();
  46. }
  47. return sb.ToString();
  48. }
  49.  
  50. }
  51. class Duplication2 : IDuplicationFinder
  52. {
  53. public string FindDuplication(int[] input, uint minTimes)
  54. {
  55. List<int> result = new List<int>();
  56. Array.Sort(input);
  57. if (minTimes == 1)//如果次数是1,就要把去重显示
  58. {
  59. if (input[0] == input[1])
  60. result.Add(input[1]);
  61. for (int i = 1; i < input.Length - 1; i++)
  62. {
  63. if (input[i] != input[i + 1])
  64. result.Add(input[i + 1]);
  65. }
  66. }
  67. else
  68. {
  69. int count = 1;//排序后 前一个跟后一个对比,所以从1开始
  70. for (int i = 0; i < input.Length - 1; i++)
  71. {
  72. if (result.Count > 0 && result.Last() == input[i])
  73. continue;
  74. if (input[i] == input[i + 1])
  75. {
  76. count++;
  77. if (count >= minTimes)
  78. {
  79. result.Add(input[i]);
  80. count = 1;
  81. }
  82. }
  83.  
  84. }
  85. }
  86.  
  87. return string.Join(",", result);
  88. }
  89. }
  90. interface IDuplicationFinder
  91. {
  92. string FindDuplication(int[] input, uint minTimes);
  93. }

  

  1. t=[1,22,33,1,44,22,11,3,224,5,6,22,1,44]//查找出现次数最多的数字和次数 ruby
  2. hst={}
  3. t.each do |item|
  4. if(hst.key?(item))
  5. hst[item]+=1
  6. else
  7. hst[item]=1
  8. end
  9. end
  10. p hst
  11. b = Hash[hst.sort_by(){ |k, v| v }.reverse]
  12. p b
  13. p b.first

数组中所有重复次数大于等于minTimes的数字的更多相关文章

  1. 【C语言】统计数字在排序数组中出现的次数

    //数字在排序数组中出现的次数. //统计一个数字在排序数组中出现的次数.比如:排序数组{1,2,3,3,3,3,4,5}和数字3,因为3出现了4次,因此输出4. #include <stdio ...

  2. JZ-037-数字在排序数组中出现的次数

    数字在排序数组中出现的次数 题目描述 统计一个数字在升序数组中出现的次数. 题目链接: 数字在排序数组中出现的次数 代码 /** * 标题:数字在排序数组中出现的次数 * 题目描述 * 统计一个数字在 ...

  3. 剑指Offer面试题:32.数字在排序数组中出现的次数

    一.题目:数字在排序数组中出现的次数 题目:统计一个数字在排序数组中出现的次数.例如输入排序数组{1,2,3,3,3,3,4,5}和数字3,由于3在这个数组中出现了4次,因此输出4. 二.解题思路 2 ...

  4. 剑指offer系列41---数字在数组中出现的次数

    [题目]统计一个数字在排序数组中出现的次数. package com.exe9.offer; /** * [题目]统计一个数字在排序数组中出现的次数. * @author WGS * */ publi ...

  5. 剑指offer——python【第37题】数字在排序数组中出现的次数

    题目描述 统计一个数字在排序数组中出现的次数 思路 最贱的方法依旧是count计数.. 当然,,看到有序数组就应该想到二分法,找到重复数字左边和右边的数字,然后两个相减就可以了 解答 方法1 coun ...

  6. 《剑指offer》第五十三题(数字在排序数组中出现的次数)

    // 面试题53(一):数字在排序数组中出现的次数 // 题目:统计一个数字在排序数组中出现的次数.例如输入排序数组{1, 2, 3, 3, // 3, 3, 4, 5}和数字3,由于3在这个数组中出 ...

  7. 剑指offer37:统计一个数字在排序数组中出现的次数

    1 题目描述 统计一个数字在排序数组中出现的次数. 2 思路和方法 (1)查找有序数组,首先考虑使用二分查找,使时间复杂度为O(log n).更改二分查找的条件,不断缩小区间,直到区间头和区间尾均为k ...

  8. Leetcode26——删除有序数组中的重复项(双指针法)

    Leetcode26--删除有序数组中的重复项(双指针法) 1. 题目简述 给你一个升序排列的数组 nums ,请你原地 删除重复出现的元素,使每个元素只出现一次 ,返回删除后数组的新长度.元素的相对 ...

  9. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

随机推荐

  1. ZOJ 1074 最大子矩阵和

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  2. 【BZOJ1030】文本生成器

    Description JSOI交给队员ZYX一个任务,编制一个称之为“文本生成器”的电脑软件:该软件的使用者是一些低幼人群,他们现在使用的是GW文本生成器v6版.该软件可以随机生成一些文章―――总是 ...

  3. 前端跨域之html5 XMLHttpRequest Level2

    前端代码 var xhr=new XMLHttpRequest(); xhr.open('POST','http://127.0.0.1:8081/ceshi',true); xhr.onreadys ...

  4. python学习笔记12(函数三): 参数类型、递归、lambda函数

    一.函数参数的类型 之前我们接触到的那种函数参数定义和传递方式叫做位置参数,即参数是通过位置进行匹配的,从左到右,依次进行匹配,这个对参数的位置和个数都有严格的要求.而在Python中还有一种是通过参 ...

  5. 简单3d RPG游戏 之 005 选择敌人

    选择一个敌人,按ctrl+d,复制出3个,调整一下它们的位置,不重叠,修改Tag为Enemy,禁用EnemyAI. 创建Targetting脚本,绑定到Player玩家对象 public class ...

  6. 【贪心】 BZOJ 3252:攻略

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 261  Solved: 90[Submit][Status][Discuss] De ...

  7. [转载]再谈iframe自适应高度

    Demo页面:主页面 iframe_a.html ,被包含页面 iframe_b.htm 和 iframe_c.html 下面开始讲: 通过Google搜索iframe 自适应高度,结果5W多条,搜索 ...

  8. WPF SplitButton 的杂七杂八

    原文: http://www.codeproject.com/Articles/20612/A-WPF-SplitButton SplitButton.cs using System; using S ...

  9. FireFly 服务端 Unity3D黑暗世界 客户端 问题

    启动服务端成功截图: 连接成功截图: 测试服务端是否启动成功: 在Web输入:http://localhost:11009/  按回车 (查看cmd启动的服务端 是否多出如下显示) 服务端启动成功.P ...

  10. 学点PYTHON基础的东东--数据结构,算法,设计模式---访问者模式

    说实话,感觉不是特别多,可能没遇到过多场面, 所以对应用场景没感觉吧. 反正,各种模式就是把类的实例传来传去,久而久之,产生了一些规律...:) # 轮子,引擎, 车身这些定义好了都不需要变动 cla ...