Skip to content

Commit

Permalink
Add solution for cw-merge-arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
skosovsky committed Dec 16, 2023
1 parent 782c13f commit d2487eb
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cw-merge-arrays/README.md
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]`
2 changes: 2 additions & 0 deletions cw-merge-arrays/go.mod
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
28 changes: 28 additions & 0 deletions cw-merge-arrays/main.go
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
}
46 changes: 46 additions & 0 deletions cw-merge-arrays/main_test.go
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)
}
})
}
}

0 comments on commit d2487eb

Please sign in to comment.