-
Notifications
You must be signed in to change notification settings - Fork 15
/
Find Sum of Two Numbers.js
48 lines (42 loc) · 1.07 KB
/
Find Sum of Two Numbers.js
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
// function findSum(array, z) {
// var n = array.length,
// m,
// i,
// j,
// numbers = {},
// newArray = [],
// currentSum;
// for (i = 0; i < n; i++) {
// if (!numbers[array[i]]) {
// numbers[array[i]] = true;
// newArray.push(array[i]);
// }
// }
// m = newArray.length;
// for (i = 0; i < m; i++) {
// for (j = m - 1; j >= i; j--) {
// currentSum = newArray[i] + newArray[j];
// if (currentSum === z) {
// return [array.indexOf(newArray[i]), array.indexOf(newArray[j])];
// }
// if (currentSum < z) {
// break;
// }
// }
// }
// }
function findSum(array, z) {
var n = array.length,
i,
numbers = {};
for (i = 0; i < n; i++) {
if (numbers[array[i]] === undefined) {
numbers[array[i]] = i;
}
}
for (i in numbers) {
if (numbers[z - i]) {
return [numbers[i], numbers[z - i]];
}
}
}