-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Merge two sorted arrays into one | ||
|
||
You are given two sorted arrays that both only contain integers. Your task is to find a way to merge them into a single one, sorted in asc order. Complete the function `mergeArrays(arr1, arr2)`, where `arr1` and `arr2` are the original sorted arrays. | ||
|
||
You don't need to worry about validation, since `arr1` and `arr2` must be arrays with `0` or more Integers. If both `arr1` and `arr2` are empty, then just return an empty array. | ||
|
||
Note: `arr1` and `arr2` may be sorted in different orders. Also `arr1` and `arr2` may have same integers. Remove duplicated in the returned result. | ||
|
||
For example: | ||
|
||
* `[1, 2, 3, 4, 5], [6, 7, 8, 9, 10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]` | ||
|
||
* `[1, 3, 5, 7, 9], [10, 8, 6, 4, 2] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]` | ||
|
||
* `[1, 3, 5, 7, 9, 11, 12], [1, 2, 3, 4, 5, 10, 12] = [1, 2, 3, 4, 5, 7, 9, 10, 11, 12]` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module github.com/skosovsky/algo/cw-merge-arrays | ||
go 1.21.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
) | ||
|
||
func main() { | ||
fmt.Println(mergeArrays([]int{}, []int{})) //nolint:gomnd | ||
} | ||
|
||
func mergeArrays(arr1 []int, arr2 []int) []int { | ||
arrs := append(arr1, arr2...) | ||
noDouble := map[int]bool{} | ||
result := []int{} | ||
|
||
for _, v := range arrs { | ||
noDouble[v] = true | ||
} | ||
|
||
for k := range noDouble { | ||
result = append(result, k) | ||
} | ||
|
||
sort.Ints(result) | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_mergeArrays(t *testing.T) { | ||
type args struct { | ||
arr1 []int | ||
arr2 []int | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []int | ||
}{ | ||
{ | ||
name: "Example 1", | ||
args: args{[]int{1, 2, 3, 4}, []int{5, 6, 7, 8}}, | ||
want: []int{1, 2, 3, 4, 5, 6, 7, 8}, | ||
}, | ||
{ | ||
name: "Example 2", | ||
args: args{[]int{1, 3, 5, 7, 9}, []int{10, 8, 6, 4, 2}}, | ||
want: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, | ||
}, | ||
{ | ||
name: "Example 3", | ||
args: args{[]int{1, 3, 5, 7, 9, 11, 12}, []int{1, 2, 3, 4, 5, 10, 12}}, | ||
want: []int{1, 2, 3, 4, 5, 7, 9, 10, 11, 12}, | ||
}, | ||
{ | ||
name: "Example 4", | ||
args: args{[]int{}, []int{}}, | ||
want: []int{}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := mergeArrays(tt.args.arr1, tt.args.arr2); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("mergeArrays() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |