给出代码

#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <memory.h>
#include <string.h> int main()
{
typedef int data_type;
data_type a[] = {1,4,6,7,7,8,4,9,1,7,6,4,1,2,0,0}; const char* hint = "The original number is:\n ";
write(STDOUT_FILENO, hint, strlen(hint));
for(int i = 0; i < sizeof(a)/sizeof(a[0]); ++i)
{
printf("%d ", a[i]);
}
printf("\n"); // get the max value
int max = a[0];
for(int i = 0;i < sizeof(a)/sizeof(data_type);++i)
{
if(a[i] > max)
{
max = a[i];
}
} // allocate an space. you can use bitwise if the space is too big.
data_type* p = new data_type[max + 1];
memset(p, 0, max + 1); int cnt = 0;
for(int i = 0;i < sizeof(a)/sizeof(data_type);++i)
{
if(*(p + a[i]) == -1)
{
++cnt;
}
else
{
*(p + a[i]) = -1;
}
} std::cout << "The dumplicated number is " << cnt << std::endl; return 0;
}

  

编译执行

[root@ba algr]# g++ array.cpp -g
[root@ba algr]# ./a.out
The original number is:
1 4 6 7 7 8 4 9 1 7 6 4 1 2 0 0
The dumplicated number is 8
[root@ba algr]#

  

求一个数组中重复数字的个数,要求复杂度为O(n)的更多相关文章

  1. 求一个数组中最小的K个数

    方法1:先对数组进行排序,然后遍历前K个数,此时时间复杂度为O(nlgn); 方法2:维护一个容量为K的最大堆(<算法导论>第6章),然后从第K+1个元素开始遍历,和堆中的最大元素比较,如 ...

  2. 求出数组中所有数字的和&&弹出层效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)

    题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description   从有序数组中移除重 ...

  4. [LeetCode169]Majority Element求一个数组中出现次数大于n/2的数

    题目: Given an array of size n, find the majority element. The majority element is the element that ap ...

  5. java二分法来求一个数组中一个值的key

    package TestArray; import java.util.Arrays; /** * 二分法查找 */ public class Test { public static void ma ...

  6. 求一个数组中第K小的数

    面试南大夏令营的同学说被问到了这个问题,我的第一反应是建小顶堆,但是据他说用的是快排的方法说是O(n)的时间复杂度, 但是后来经过我的考证,这个算法在最坏的情况下是O(n^2)的,但是使用堆在一般情况 ...

  7. Java求一个数组中的最大值和最小值

    原创作品,转载请注明出处:https://www.cnblogs.com/sunshine5683/p/9927186.html 今天在工作中遇到对一个已知的一维数组取出其最大值和最小值,分别用于参与 ...

  8. js 算法,判断一个数组中的数字出现多少次

    let arr = [11, 11, 2, 2, 5, 5, 5, 5, 3]; //创建一个map,把每个数字和其个数相对应 let countObj = {}; for (i = 0; i < ...

  9. spoj 694 求一个字符串中不同子串的个数

    SPOJ Problem Set (classical) 694. Distinct Substrings Problem code: DISUBSTR Given a string, we need ...

随机推荐

  1. Android Camera2 预览功能实现

    1. 概述 最近在做一些关于人脸识别的项目,需要用到 Android 相机的预览功能.网上查阅相关资料后,发现 Android 5.0 及以后的版本中,原有的 Camera API 已经被 Camer ...

  2. FIDDLER的使用方法及技巧总结

    转自: https://www.cnblogs.com/ink-marks/p/6363275.html 一.FIDDLER快速入门及使用场景 Fiddler的官方网站:http://www.fidd ...

  3. Ubuntu下crontab启动、重启、关闭命令

    在Ubuntu14.04环境下,利用crontab编写shell脚本程序,定时执行php相关程序.在这个过程中,经常使用到的crontab命令如下: (root权限下) crontab启动:/etc/ ...

  4. Bootstrap -- 表格样式、表单布局

    Bootstrap -- 表格样式.表单布局 1. 表格的一些样式 举例: <!DOCTYPE html> <html> <head> <meta http- ...

  5. Hbase技术笔记

    一.Hbase介绍 二.Hbase的Region介绍 三.Hbase的写逻辑介绍 四.Hbase的故障恢复 五.Hbase的拆分和合并 如下ppt所示: 下面就来针对各个部分的内容来进行详细的介绍: ...

  6. Android图片选择---MultiImageSelector的使用

    Github地址:https://github.com/lovetuzitong/MultiImageSelector MultiImageSelector主要是图片选择功能. AndroidStud ...

  7. dotnetcore ueditor

    dotnetcore ueditor  https://github.com/durow/ueditornetcore http://www.cnblogs.com/durow/p/6116393.h ...

  8. Logging 日志配置格式模板

    import osBASE_DIR = os.path.dirname(os.path.dirname(__file__))DB_PATH = os.path.join(BASE_DIR, 'db') ...

  9. redhat 6.5 安装和配置zabbix客户端

    一.安装zabbix-agent端 rpm -ivh http://repo.zabbix.com/zabbix/2.4/rhel/6/x86_64/zabbix-release-2.4-1.el6. ...

  10. Spring Security(三十七):Part IV. Web Application Security

    Most Spring Security users will be using the framework in applications which make user of HTTP and t ...