给出代码

#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. 通过apt-get安装JDK8

    安装python-software-properties $sudo apt-get install python-software-properties $sudo apt-get install ...

  2. 统计numpy数组中最频繁出现的值

    arr = np.array([[1,2,100,4,5,6],[1,1,100,3,5,5],[2,2,4,4,6,6]]) 方法一: count = np.bincount(arr[:,2]) # ...

  3. docker compose 服务启动顺序控制

    概要 docker-compose 可以方便组合多个 docker 容器服务, 但是, 当容器服务之间存在依赖关系时, docker-compose 并不能保证服务的启动顺序. docker-comp ...

  4. nuxt axios代理

    modules: [ '@nuxtjs/axios', ], axios: { //prefix: '/api/', proxy: true // Can be also an object with ...

  5. SpringCloud 学习网址记录

    SpringCloud Gateway https://www.cnblogs.com/ityouknow/p/10141740.html 熔断降级的概念 https://blog.csdn.net/ ...

  6. mybatis 错误

    org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyR ...

  7. python3 今日大纲 day05

    1. 上周内容回顾 1. 闭包: 内层函数对外层函数变量的使用 def outer(): a = 10 def inner(): print(a) return inner ret = outer() ...

  8. 使用CompletableFuture优化你的代码执行效率

    这篇文章详细讲解java8中CompletableFuture的特性,方法以及实例. 在java8以前,我们使用java的多线程编程,一般是通过Runnable中的run方法来完成,这种方式,有个很明 ...

  9. 什么是面向切面编程AOP

    一丶前言 看过一些描述关于AOP切面编程的文章,写的太概念化让人很难理解,下面是我自己的理解,希望能帮到新人,如有错误欢迎指正. 二丶AOP是什么,它的应用场景是什么? AOP也跟IOC,OOP这些思 ...

  10. C#中字节数组(byte[])和字符串相互转换

    转换过程主要使用到System.Text.Encoding命名空间下的类 1. 字符串转换成字节数组byte[]: string str = "This is test string&quo ...