![在这里插入图片描述](https://img-blog.csdnimg.cn/9cf56fe2679347f7b5346b9d4fc7d9fc.jpeg)
- /方法1:先排序,在依次比较相邻是否相等(会超时)/
#include <iostream>
#include <vector>
using namespace std;
void PritnLn(vector<int>& vec)
{
for (auto aa : vec)
{
cout << aa << " ";
}
cout << "\n";
}
void Swap_Value(vector<int>& vec,int a,int b)
{
int tmp = vec[a];
vec[a] = vec[b];
vec[b] = tmp;
}
int Find_Value(vector<int>& vec, int low, int high)
{
int privot_value = vec[low];
while (low < high)
{
if (low < high && privot_value <= vec[high])
high--;
Swap_Value(vec,low,high);
if (low<high && privot_value >= vec[low])
low++;
Swap_Value(vec, low, high);
}
return low;
}
void Quick_Sort(vector<int>& vec, int left, int right)
{
if (left > right) return;
int privot_key = Find_Value(vec, left, right);
Quick_Sort(vec,left,privot_key-1);
Quick_Sort(vec,privot_key+1,right);
}
int Dup_Value(vector<int>& vec)
{
Quick_Sort(vec, 0, vec.size() - 1);
PritnLn(vec);
for (int i = 0; i+1 < vec.size(); i++)
{
if (vec[i] == vec[i + 1])
return vec[i + 1];
}
}
void main(void)
{
vector<int> vec1 = {2,3,1,0,2,5,3};
PritnLn(vec1);
cout<< Dup_Value(vec1)<<endl;
}
- 方法2:计数数组HashTable或者直接unordermap容器使用*/
#include <iostream>
#include <vector>
using namespace std;
int Dup_Value(vector<int>& vec)
{
vector<int> HashTable(vec.size(), 0);
for (int i = 0; i < vec.size(); i++)
{
HashTable[vec[i]]++;
if ((HashTable[vec[i]]) > 1)
return vec[i];
}
}
void main(void)
{
vector<int> vec1 = { 2,3,1,0,2,5,3 };
cout << Dup_Value(vec1) << endl;
}