Skip to content

Commit 2d99be0

Browse files
committed
Day - 90 work
1 parent 1c305d4 commit 2d99be0

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 128 |
10-
| Current Streak | 89 days |
11-
| Longest Streak | 89 ( August 17, 2015 - November 13, 2015 ) |
9+
| Total Problems | 129 |
10+
| Current Streak | 90 days |
11+
| Longest Streak | 90 ( August 17, 2015 - November 14, 2015 ) |
1212

1313
</center>
1414

@@ -154,6 +154,7 @@ Include contains single header implementation of data structures and some algori
154154
| Problem | Solution |
155155
| :------------ | :----------: |
156156
| Given a sorted vector, return first index of the occurrence of a value in vector, if number does not exist, return -1 | [first_occurrence_binary_search.cpp](sort_search_problems/first_occurrence_binary_search.cpp) |
157+
| 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.| [firstRepeatingElement.cpp](sort_search_problems/firstRepeatingElement.cpp)|
157158
| Given a list of unsorted integers, A={a<sub>1</sub>,a<sub>2</sub>,…,a<sub>N</sub>}, Find the pair of elements that have the smallest absolute difference between them? If there are multiple pairs, find them all.| [closest_numbers.cpp](sort_search_problems/closest_numbers.cpp)|
158159
| Given a sorted array, determine index of fixed point in this array. If array does not have a fixed point return -1. An array has a fixed point when index of the element is same as index i.e. i == arr[i], Expected time complexity O(logn)| [fixedPoint.cpp](sort_search_problems/fixedPoint.cpp)|
159160
| Find the maximum element in an array which is first increasing and then decreasing. Input: arr[] = {8, 10, 20, 80, 100, 200, 400, 500, 3, 2, 1}, output : 500. Array may be strictly increasing or decreasing as well. ExpectedTime complexity is O(logn).| [findMaximum.cpp](sort_search_problems/findMaximum.cpp)|
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Find the first repeating element in an array of integers
3+
* Given an array of integers, find the first repeating element in it.
4+
* We need to find the element that occurs more than once and whose index of first occurrence is smallest.
5+
* Examples:
6+
*
7+
* Input: arr[] = {10, 5, 3, 4, 3, 5, 6}
8+
* Output: 5 [5 is the first element that repeats]
9+
*
10+
* Input: arr[] = {6, 10, 5, 4, 9, 120, 4, 6, 10}
11+
* Output: 6 [6 is the first element that repeats]
12+
*/
13+
14+
15+
#include <iostream>
16+
#include <vector>
17+
#include <unordered_set>
18+
#include <limits>
19+
20+
int firstRepeating(std::vector<int> & arr) {
21+
std::unordered_set<int> arrSet;
22+
int min = -1;
23+
int arrSize = (int) arr.size();
24+
for ( int i = arrSize -1; i >= 0; i--) {
25+
if ( arrSet.find(arr[i]) != arrSet.end()) {
26+
min = i;
27+
} else {
28+
arrSet.insert(arr[i]);
29+
}
30+
}
31+
if ( min != -1 ) {
32+
return arr[min];
33+
} else {
34+
return std::numeric_limits<int>::min();
35+
}
36+
}
37+
38+
39+
void printArray( std::vector<int> & arr ) {
40+
std::cout << "Array :";
41+
for (auto a : arr) {
42+
std::cout << a << " ";
43+
}
44+
std::cout << std::endl;
45+
}
46+
47+
48+
int main() {
49+
std::vector<int> arr{6, 10, 5, 4, 9, 120, 4, 6, 10};
50+
printArray(arr);
51+
std::cout << "First repeating element in above array is:" << firstRepeating(arr) << std::endl;
52+
53+
std::vector<int> arr2 {10, 5, 3, 4, 3, 5, 6};
54+
printArray(arr2);
55+
std::cout << "First repeating element in above array is:" << firstRepeating(arr2) << std::endl;
56+
return 0;
57+
}

sort_search_problems/run

32.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)