查看一个int数组里边的每个数字出现过几次

public void aa()
{
  int[] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1 };
  Hashtable ht = new Hashtable();
  for (int i = 0; i < a.Length; i++)
  {
    if (ht.ContainsKey(a[i]))
    {
      ht.Add(a[i], (int)ht[i] + 1);
    }
    else
    {
      ht.Add(a[i], 1);
    }
  }
  string str = "";
  foreach (DictionaryEntry item in ht)
  {
    str += item.Key + ":" + item.Value + " ";
  }
}

  

时间: 2024-08-01 04:42:44

查看一个int数组里边的每个数字出现过几次的相关文章

(笔试题)把一个整数数组中重复的数字去掉

题目: 把一个整数数组中重复的数字去掉,并输出剩下的不重复的元素.(要求不能开辟新空间) 思路: 先排序,然后遍历数组比较,详见代码 代码: #include <iostream> #include <algorithm> using namespace std; int cmp(const void* a,const void* b){ return (*(int*)a-*(int*)b); } int unique(int* a,int n){ int k=0; for(int

产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复

写在前面 前天去面试了,给出的笔试中有这样的一道算法题,产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复 当时,脑子一热,也没想那么多,就用集合实现了一下,经面试官提醒,发现还有更好的方式来实现. 代码 首先看一下这样一段代码 1 namespace Wolfy.RandomDemo 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 List<int> lst = new List<i

黑马程序员——java--将一个int数组转换成一个字符串

将一个int 数组转换成字符串 并输出在控制台上 //将一个int 数组转换成一个字符串 public class IntToStringDemo { public static void main(String[] args) { // TODO Auto-generated method stub //定义一个int类型的数组 int[] i ={4,5,8,6,5,8,7,4,5}; //调用自定义方法将int数组的方法转换成字符串 toStringMethod(i); } private

Java -- 给定一个int数组,拼接出最大数值

public class ZhiJieTiaoDong { /* 给定一个数组:组合成最大数值 */ public String szpj(int[] args){ if(null == args || args.length == 0){ return ""; } StringBuilder stringBuilder = new StringBuilder(); int[] yxsz = compareSZ(args); for (int i = args.length - 1;

Random 产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复。

做法1: List<int> list = new List<int>(); Random rand = new Random();  while (list.Count < 100)             {                 int number = rand.Next(1, 101);//>=1,<101 if (!list.Contains(number))//如果list中已经含有这个数,则不插入 { list.Add(number);

怎么把一个int数组转化为char型数组??

/* 234 Press any key to continue */ #include <stdio.h> int main() { int i,num = 234,n; char ch,s[10]; for(n = 0; num; ++n) { s[n] = num % 10 + '0'; num /= 10; } s[n] = '\0'; for(i = 0; i < n / 2; ++i) { ch = s[i]; s[i] = s[n - 1 - i]; s[n - 1 - i

signal num一个int数组,里面数值都是成对出现,只有一个是单独的,找出单独者。360ms

public class Solution { public int singleNumber(int[] nums) { int ret = nums[0]; for (int i = 1; i < nums.length; i++) { ret ^= nums[i]; } return ret; } }

49 数组中重复的数字 记忆

题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2. 思路:1)直接排序: 2)使用哈希表,比如开辟一个布尔数组,判断该数字在之前是否已经访问过,vector<bool> visit(n,false);已经访问过,则找到这个重复数字. 3)时间复杂度是O(n),空间复杂度是

在数组中随机插入数字且不重复

产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复.(两种方法) 1, List<int> myList = new List<int>(); Random ran = new Random(); while (myList.Count<100) { int num = ran.Next(1, 101); if (!myList.Contains(num)) { myList.Add(num); } } foreach (int item in myL