FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript-Interview-Question/PairWithGivenSum.js at master · plopfizz/JavaScript-Interview-Question · GitHub
plopfizz
/
JavaScript-Interview-Question
Public
forked from
namitmalasi/JavaScript-Interview-Question
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript-Interview-Question
/
PairWithGivenSum.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
33 lines (27 loc) · 604 Bytes
Breadcrumbs
JavaScript-Interview-Question
/
PairWithGivenSum.js
Copy path
File metadata and controls
33 lines (27 loc) · 604 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
31
32
33
// Approach 1
function
PairWithGivenSum
(
arr
,
sum
)
{
for
(
let
i
=
0
;
i
<
arr
.
length
;
i
++
)
{
for
(
let
j
=
i
+
1
;
j
<
arr
.
length
;
j
++
)
{
if
(
arr
[
i
]
+
arr
[
j
]
===
sum
)
{
return
true
;
}
}
}
return
false
;
}
const
arr
=
[
2
,
3
,
3
,
6
]
;
console
.
log
(
PairWithGivenSum
(
arr
,
6
)
)
;
// Approach 2
function
PairWithGivenSum
(
arr
,
sum
)
{
const
set
=
new
Set
(
)
;
for
(
let
i
=
0
;
i
<
arr
.
length
;
i
++
)
{
if
(
set
.
has
(
sum
-
arr
[
i
]
)
)
{
return
true
;
}
else
{
set
.
add
(
arr
[
i
]
)
;
}
}
return
false
;
}
const
arr
=
[
2
,
1
,
3
,
6
]
;
console
.
log
(
PairWithGivenSum
(
arr
,
6
)
)
;
Back
|
FazBrowse Home
|
New Git URL