算法找出重复的数字数组中的

编程入门 行业动态 更新时间:2024-10-10 04:24:01
本文介绍了算法找出重复的数字数组中的---用最快的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要最快和简单的算法查找重复的数字在一个阵列,还应该能知道重复数

I need the fastest and simple algorithm which finds the duplicate numbers in an array, also should be able to know the number of duplicates.

例如:如果数组 {2,3,4,5,2,4,6,2,4,7,3,8,2}

我应该能知道,有4个2的,两个3的和三个4的

I should be able to know that there are four 2's, two 3's and three 4's.

推荐答案

这里的,做它与标准输入C版本;它以最快的速度输入的长度(注意,参数的命令行上的数量是有限的......),但应该给你如何进行一个想法:

here's a C version that does it with standard input; it's as fast as the length of the input (beware, the number of parameters on the command line is limited...) but should give you an idea on how to proceed:

#include <stdio.h> int main ( int argc, char **argv ) { int dups[10] = { 0 }; int i; for ( i = 1 ; i < argc ; i++ ) dups[atoi(argv[i])]++; for ( i = 0 ; i < 10 ; i++ ) printf("%d: %d\n", i, dups[i]); return 0; }

例如用法:

$ gcc -o dups dups.c $ ./dups 0 0 3 4 5 0: 2 1: 0 2: 0 3: 1 4: 1 5: 1 6: 0 7: 0 8: 0 9: 0

警告:

  • 如果您打算也算10S,11S的数量,等等 - >复本[]数组必须做大

  • if you plan to count also the number of 10s, 11s, and so on -> the dups[] array must be bigger

作为一个练习是实现从一个整数数组读取并确定自己的位置

left as an exercise is to implement reading from an array of integers and to determine their position

更多推荐

算法找出重复的数字数组中的

本文发布于:2023-11-30 18:21:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651043.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法   组中   字数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!