forked from mandliya/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firstRepeatingElement.cpp
57 lines (49 loc) · 1.38 KB
/
firstRepeatingElement.cpp
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
48
49
50
51
52
53
54
55
56
57
/*
* Find the first repeating element in an array of integers
* Given an array of integers, find the first repeating element in it.
* We need to find the element that occurs more than once and whose index of first occurrence is smallest.
* Examples:
*
* Input: arr[] = {10, 5, 3, 4, 3, 5, 6}
* Output: 5 [5 is the first element that repeats]
*
* Input: arr[] = {6, 10, 5, 4, 9, 120, 4, 6, 10}
* Output: 6 [6 is the first element that repeats]
*/
#include <iostream>
#include <vector>
#include <unordered_set>
#include <limits>
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();
}
}
void printArray( std::vector<int> & arr ) {
std::cout << "Array :";
for (auto a : arr) {
std::cout << a << " ";
}
std::cout << std::endl;
}
int main() {
std::vector<int> arr{6, 10, 5, 4, 9, 120, 4, 6, 10};
printArray(arr);
std::cout << "First repeating element in above array is:" << firstRepeating(arr) << std::endl;
std::vector<int> arr2 {10, 5, 3, 4, 3, 5, 6};
printArray(arr2);
std::cout << "First repeating element in above array is:" << firstRepeating(arr2) << std::endl;
return 0;
}