数组中重复的个数

题目:给一个数组,及数组的长度,输出数组中重复数字的个数

//
//  array_repeat.cpp
//  offer_test
//
//  Created by caixiao on 2017/8/6.
//  Copyright © 2017年 caixiao. All rights reserved.

//  array_repeat

#include <stdio.h>
#include<map>
using namespace std;

//compute repeat array
int Arr_rep(int Array[],int length)
{

    int count = 0;
    int i;
    map<int,int> rep_map;
    map<int,int>::iterator it;
    for(i=0;i < length;i++){
        it = rep_map.find(Array[i]);
        if(it != rep_map.end()){
            if (rep_map[Array[i]] == 1) {
                count++;
            }
            rep_map[Array[i]]++;
        }
        else{
            rep_map[Array[i]] = 1;
        }
    }
    return count;
}

与原始的数组间数字交换处理不同,可拓展性增强,空间复杂度高

测试case:
1、数组中没有重复的数字
2、数组中全是重复的数字
3、空的数组(待处理)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值