1+ /**
2+ * 2722. Join Two Arrays by ID
3+ * https://leetcode.com/problems/join-two-arrays-by-id/
4+ * Difficulty: Medium
5+ *
6+ * Given two arrays arr1 and arr2, return a new array joinedArray. All the objects in each
7+ * of the two inputs arrays will contain an id field that has an integer value.
8+ *
9+ * joinedArray is an array formed by merging arr1 and arr2 based on their id key. The length
10+ * of joinedArray should be the length of unique values of id. The returned array should be
11+ * sorted in ascending order based on the id key.
12+ *
13+ * If a given id exists in one array but not the other, the single object with that id should
14+ * be included in the result array without modification.
15+ *
16+ * If two objects share an id, their properties should be merged into a single object:
17+ * - If a key only exists in one object, that single key-value pair should be included in
18+ * the object.
19+ * - If a key is included in both objects, the value in the object from arr2 should override
20+ * the value from arr1.
21+ */
22+
23+ /**
24+ * @param {Array } arr1
25+ * @param {Array } arr2
26+ * @return {Array }
27+ */
28+ var join = function ( arr1 , arr2 ) {
29+
30+ } ;
31+
32+
33+ arr1 = [
34+ { "id" : 1 , "x" : 1 } ,
35+ { "id" : 2 , "x" : 9 }
36+ ] ,
37+ arr2 = [
38+ { "id" : 3 , "x" : 5 }
39+ ]
40+
41+ ex1 = join ( arr1 , arr2 )
42+ console . log ( ex1 )
43+ // [
44+ // {"id": 1, "x": 1},
45+ // {"id": 2, "x": 9},
46+ // {"id": 3, "x": 5}
47+ // ]
48+
49+
50+ arr1 = [
51+ { "id" : 1 , "x" : 2 , "y" : 3 } ,
52+ { "id" : 2 , "x" : 3 , "y" : 6 }
53+ ] ,
54+ arr2 = [
55+ { "id" : 2 , "x" : 10 , "y" : 20 } ,
56+ { "id" : 3 , "x" : 0 , "y" : 0 }
57+ ]
58+
59+ ex2 = join ( arr1 , arr2 )
60+ console . log ( ex2 )
61+ // [
62+ // {"id": 1, "x": 2, "y": 3},
63+ // {"id": 2, "x": 10, "y": 20},
64+ // {"id": 3, "x": 0, "y": 0}
65+ // ]
66+
67+ arr1 = [
68+ { "id" : 1 , "b" : { "b" : 94 } , "v" : [ 4 , 3 ] , "y" : 48 }
69+ ] ,
70+ arr2 = [
71+ { "id" : 1 , "b" : { "c" : 84 } , "v" : [ 1 , 3 ] }
72+ ]
73+
74+ ex3 = join ( arr1 , arr2 )
75+ console . log ( ex3 )
76+ // [
77+ // {"id": 1, "b": {"c": 84}, "v": [1, 3], "y": 48 }
78+ // ]
0 commit comments