题目:给一个数组,及数组的长度,输出数组中重复数字的个数
//
// 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、空的数组(待处理)