MCPcopy
hub / github.com/HuberTRoy/leetCode / merge

Function merge

Array/KthLargestElementInAnArray.js:45–74  ·  view source on GitHub ↗
(arr, arr2)

Source from the content-addressed store, hash-verified

43}
44
45function merge(arr, arr2) {
46 let result = []
47
48 let index1 = 0
49 let index2 = 0
50
51 while (index1 <= arr.length - 1 && index2 <= arr2.length - 1) {
52 if (arr[index1] < arr2[index2]) {
53 result.push(arr[index1])
54 index1 += 1
55 } else if (arr[index1] > arr2[index2]) {
56 result.push(arr2[index2])
57 index2 += 1
58 } else {
59 result.push(arr[index1], arr2[index2])
60 index1 += 1
61 index2 += 1
62 }
63 }
64 // 如果第一个arr
65 if (index1 <= arr.length - 1) {
66 result.push(...arr.slice(index1))
67 }
68
69 if (index2 <= arr2.length - 1) {
70 result.push(...arr2.slice(index2))
71 }
72
73 return result
74}
75
76function sort2(arr) {
77 if (arr.length <= 8) {

Callers 1

sort2Function · 0.85

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected