Skip to content

Commit 282e92a

Browse files
committed
Day 62 - first non repeating int
1 parent 2cd7782 commit 282e92a

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-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 | 90 |
10-
| Current Streak | 61 days |
11-
| Longest Streak | 61 ( August 17, 2015 - October 16, 2015 ) |
9+
| Total Problems | 91 |
10+
| Current Streak | 62 days |
11+
| Longest Streak | 62 ( August 17, 2015 - October 17, 2015 ) |
1212

1313
</center>
1414

@@ -126,6 +126,7 @@ Include contains single header implementation of data structures and some algori
126126
| Print the contents of matrix in a spiral order | [matrix_spiral_print.cpp](common_ds_problems/matrix_spiral_print.cpp)
127127
| Given a M x N matrix, rotate it by R rotations anticlockwise, and show the resulting matrix. | [rotate_matrix.cpp](common_ds_problems/rotate_matrix.cpp)|
128128
| Rotate an array by r elements ( left or right ) | [array_rotation.cpp](common_ds_problems/array_rotation.cpp)
129+
| Given an array of repeating/non-repeating intergeres, determine the first non-repeating int in this array | [first_non_repeating_int.cpp](common_ds_problems/first_non_repeating_int.cpp)|
129130

130131
### Math Problems
131132
| Problem | Solution |
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Given an array of integers, determine the first non repeating integer in the array.
3+
*/
4+
5+
#include<iostream>
6+
#include<map>
7+
8+
9+
int nonRepeating(int *arr, int size){
10+
std::map<int, int> m;
11+
std::map<int,int>::iterator it;
12+
for(int i = 0; i < size; ++i){
13+
it = m.find(arr[i]);
14+
if( it != m.end()){
15+
m[arr[i]] = ++(it->second);
16+
}
17+
else{
18+
m[arr[i]] = 1;
19+
}
20+
}
21+
22+
for(int i = 0; i < size; ++i){
23+
it = m.find(arr[i]);
24+
if( it != m.end()){
25+
if(it->second == 1)
26+
return arr[i];
27+
}
28+
}
29+
return -1;
30+
31+
}
32+
33+
int main(){
34+
int size;
35+
int *arr;
36+
std::cout<<"Enter size:";
37+
std::cin>>size;
38+
arr = new int[size];
39+
std::cout<<"Enter contents of array:";
40+
for(int i = 0; i < size; ++i){
41+
std::cin>>arr[i];
42+
}
43+
std::cout<<"First Non repeating integer in array:"<< nonRepeating(arr,size)<<std::endl;
44+
delete[] arr;
45+
return 0;
46+
}

0 commit comments

Comments
 (0)