FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
coding-Interview/FindAllDuplicatesInArray.java at master · arjunmullick/coding-Interview · GitHub
arjunmullick
/
coding-Interview
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
3
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
coding-Interview
/
FindAllDuplicatesInArray.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
30 lines (24 loc) · 895 Bytes
Breadcrumbs
coding-Interview
/
FindAllDuplicatesInArray.java
Copy path
File metadata and controls
30 lines (24 loc) · 895 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
com
.
leetcode
;
import
java
.
util
.
LinkedList
;
import
java
.
util
.
List
;
public
class
FindAllDuplicatesInArray
{
//https://leetcode.com/problems/find-all-duplicates-in-an-array/
class
Solution
{
// [4,3,2,7,8,2,3,1]
//8/2 = 4 - 6 , 2 - 3 ,1 - 1 , 2 - found
// start another bs l = 3 r = 8 and l =1 r =1 so we are stuck . for O(n) change array itself to use index value
// [4,3,2,7,8,2,3,1] -
public
List
<
Integer
>
findDuplicates
(
int
[]
nums
) {
List
<
Integer
>
result
=
new
LinkedList
<>();
for
(
int
i
=
0
;
i
<
nums
.
length
;
i
++){
int
val
=
nums
[
i
];
int
absVal
=
Math
.
abs
(
val
);
if
(
nums
[
absVal
-
1
] <
0
){
result
.
add
(
absVal
);
}
nums
[
absVal
-
1
] = -
1
*
nums
[
absVal
-
1
];
}
return
result
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL