time limit per test
1 second
memory limit per test
256 megabytes
Given an array aa of nn elements, print any value that appears at least three times or print -1 if there is no such value.
Input
The first line contains an integer tt (1≤t≤1041≤t≤104) — the number of test cases.
The first line of each test case contains an integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of the array.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n) — the elements of the array.
It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.
Output
For each test case, print any value that appears at least three times or print -1 if there is no such value.
Example
Input
Copy
7
1
1
3
2 2 2
7
2 2 3 3 4 2 2
8
1 4 3 4 3 2 4 1
9
1 1 1 2 2 2 3 3 3
5
1 5 2 4 3
4
4 4 4 4
Output
Copy
-1 2 2 4 3 -1 4
Note
In the first test case there is just a single element, so it can't occur at least three times and the answer is -1.
In the second test case, all three elements of the array are equal to 22, so 22 occurs three times, and so the answer is 22.
For the third test case, 22 occurs four times, so the answer is 22.
For the fourth test case, 44 occurs three times, so the answer is 44.
For the fifth test case, 11, 22 and 33 all occur at least three times, so they are all valid outputs.
For the sixth test case, all elements are distinct, so none of them occurs at least three times and the answer is -1.
每个测试的时间限制1秒
每个测试的内存限制256兆字节
给定一个包含n个元素的数组a,打印至少出现三次的任何值,如果没有这样的值,则打印-1。
输入
第一行包含一个整数t(1≤t≤104)——测试用例的数量。
每个测试用例的第一行包含一个整数n(1≤n≤2⋅105)——数组的长度。
每个测试用例的第二行包含n个整数a1,a2,…,an(1≤ai≤n)——数组的元素。
保证所有测试用例的n之和不超过2⋅105。
输出
对于每个测试用例,打印至少出现三次的任何值,如果没有这样的值,则打印-1。
示例
InputCopy
7
1
1
3
2 2 2
7
2 2 3 3 4 2 2
8
1 4 3 4 3 2 4 1
9
1 1 1 2 2 2 3 3 3
5
1 5 2 4 3
4
4 4 4 4
OutputCopy
-1
2
2
4
3
-1
4
注意
在第一个测试用例中只有一个元素,因此它不能至少出现三次,答案是 -1。
在第二个测试用例中,数组的所有三个元素都等于 2
,因此 2
出现三次,因此答案是 2
。
对于第三个测试用例,2
出现四次,因此答案是 2
。
对于第四个测试用例,4
出现三次,因此答案是 4
。
对于第五个测试用例,1
、2
和 3
都至少出现三次,因此它们都是有效输出。
对于第六个测试用例,所有元素都是不同的,因此它们都没有出现至少三次,答案是 -1。
代码:
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;void solve() {int t;cin >> t;while (t--) {int n;cin >> n;vector<int> arr(n);for (int i = 0; i < n; i++) {cin >> arr[i];}unordered_map<int, int> freq;for (int i = 0; i < n; i++) {freq[arr[i]]++;}bool found = false;for (auto& p : freq) {if (p.second >= 3) {cout << p.first << endl;found = true;break;}}if (!found) {cout << -1 << endl;}}
}int main() {solve();return 0;
}