forked from mandliya/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestFRE.cpp
More file actions
47 lines (42 loc) · 1.02 KB
/
Copy pathtestFRE.cpp
File metadata and controls
47 lines (42 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include <vector>
#include <unordered_set>
#include <limits>
using namespace std;
int firstRepeating(std::vector<int> & arr) {
std::unordered_set<int> arrSet;
int min = -1;
int arrSize = (int) arr.size();
for ( int i = arrSize -1; i >= 0; i--) {
if ( arrSet.find(arr[i]) != arrSet.end()) {
min = i;
} else {
arrSet.insert(arr[i]);
}
}
if ( min != -1 ) {
return arr[min];
} else {
return std::numeric_limits<int>::min();
}
}
TEST_CASE("Find first repeat 1"){
vector<int> arr1{234, 453465, 234, 6345, 14123, 624, 44, 34, 26, 7345 };
REQUIRE( firstRepeating(arr1) == 234 );
}
TEST_CASE("Find first repeat 2"){
vector<int> arr1{234, 453465, 6345, 14123, 624, 44, 34, 26, 7345 };
int theLimit = std::numeric_limits<int>::min();
REQUIRE( firstRepeating(arr1) == theLimit );
}
TEST_CASE("Find first repeat 3"){
vector<int> arr1;
for(int i = 0; i < 1000; i++){
if(i == 44){
arr1.push_back(44);
}
arr1.push_back(i);
}
REQUIRE( firstRepeating(arr1) == 44 );
}