-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommonElement.js
More file actions
40 lines (31 loc) · 885 Bytes
/
commonElement.js
File metadata and controls
40 lines (31 loc) · 885 Bytes
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
// Write code to create a function that accepts two arrays of numbers
// There will be one number common to both arrays
// Return the common number
// You may not use the `indexOf` or `includes` method
var commonElement = function(arrA, arrB) {
var elements = {};
for (var i = 0; i < arrA.length; i++) {
var num = arrA[i];
elements[num] = true;
}
for (var i = 0; i < arrB.length; i++) {
var num = arrB[i];
if (elements[num] === true) {
return num;
}
}
};
// This could also be achieved using a Set data structure
// var commonElement = function(arrA, arrB) {
// var elements = new Set();
// for (var i = 0; i < arrA.length; i++) {
// var num = arrA[i];
// elements.add(num);
// }
// for (var i = 0; i < arrB.length; i++) {
// var num = arrB[i];
// if (elements.has(num)) {
// return num;
// }
// }
// };