唯一最小数
题目描述:
给定一个长度为 n 的整数数组 a1,a2,…,an。
请你找到数组中只出现过一次的数当中最小的那个数。
输出找到的数的索引编号。
a1 的索引编号为 1,a2 的索引编号为 2,…,an 的索引编号为 n。
输入格式
第一行包含整数 T,表示共有 T 组测试数据。
每组数据第一行包含整数 n。
第二行包含 n 个整数 a1,a2,…,an。
输出格式
每组数据输出一行结果,即满足条件的数的索引编号,如果不存在满足条件的数,则输出 −1 。
数据范围
1≤T≤2×104,
1≤n≤2×105,
1≤ai≤n,
同一测试点内的所有 n 的和不超过 2×105。
输入样例:
6
2
1 1
3
2 1 3
4
2 2 2 3
1
1
5
2 3 2 4 2
6
1 1 5 5 4 4
输出样例:
-1
2
4
1
2
-1
思路:模拟题,重点考察对哈希表的掌握程度。
CODE:
#include<iostream>
#include<cstdio>
#include<map>
#define ll long long
#define INF 0x3f3f3f3f
#define x first
#define y second
using namespace std;
map<ll,ll> ma;
pair<ll,ll> a[200100],ans;
ll n,t;
int main()
{
ios::sync_with_stdio(false);
cin >> t;
while(t--)
{
ans.x = INF;
ma.clear();
cin >> n;
for(int i = 1;i <= n;++i)
{
cin >> a[i].x;
++ma[a[i].x];
a[i].y = i;
}
for(int i = 1;i <= n;++i)
{
if(ma[a[i].x] > 1) continue;
if(a[i].x < ans.x)
{
ans = a[i];
}
}
if(ans.x == INF) cout << "-1" << endl;
else cout << ans.y << endl;
}
return 0;
}
感谢您的阅读。