减少数组长度

huangapple go评论92阅读模式
英文:

Reduce array length

问题

尝试将两个字符串数组合并为一个数组。结果数组应该删除所有重复的元素。

func MergeArrays(str1, str2 []string) []string {
    c := make([]string, len(str1)+len(str2), cap(str1)+cap(str2))
    k := make(map[string]bool)
    for i, s := range str1 {
        if _, ok := k[s]; !ok {
            c[i] = s
            k[s] = true
        }
    }
    for j, s := range str2 {
        if _, ok := k[s]; !ok {
            c[j+len(str1)] = s
            k[s] = true
        }
    }
    return c
}

测试数据

str1 := []string{"a", "b"}
str2 := []string{"c", "d", "a"}

输出:
"a", "b", "c", "d"
数组的长度为5

我得到了我想要的输出,但数组的长度应该是4而不是5。我可以理解为什么会打印5,但我希望输出的数组长度为4
是否有其他方法可以合并两个数组?

英文:

Am trying to merge two string array to one. The resulting array should have all duplicates element removed.

func MergeArrays(str1, str2 []string) []string {
	c := make([]string, len(str1)+len(str2), cap(str1)+cap(str2))
	k := make(map[string]bool)
	for i, s := range str1 {
		if _, ok := k
展开收缩
; !ok { c[i] = s k
展开收缩
= true } } for j, s := range str2 { if _, ok := k
展开收缩
; !ok { c[j+len(str1)] = s k
展开收缩
= true } } return c }

Test Data

str1 := []string{"a", "b"}
str2 := []string{"c", "d", "a"}

Output:
"a", "b", "c", "d"
length of the array "5"

Am getting the output i want, but the length of the array should be 4 and not 5. I can understand why it prints 5, but i want the output array of length 4.
Is there other way to merge two arrays.

答案1

得分: 2

它们是切片,而不是数组,这里有一个示例可以解决你的用例:

package main

import "fmt"

func MergeStringSlices(str1, str2 []string) []string {
    m := make([]string, len(str1), len(str1)+len(str2))
    copy(m, str1)
already_seen:
    for _, s := range str2 {
        for _, t := range m {
            if s == t {
                continue already_seen
            }
        }
        m = append(m, s)
    }
    return m
}

func main() {
    str1 := []string{"a", "b"}
    str2 := []string{"c", "d", "a"}
    fmt.Printf("%#v\n", MergeStringSlices(str1, str2))
}
英文:

They're slices, not arrays and here's an example that solves your use-case:

package main

import "fmt"

func MergeStringSlices(str1, str2 []string) []string {
	m := make([]string, len(str1), len(str1)+len(str2))
	copy(m, str1)
already_seen:
	for _, s := range str2 {
		for _, t := range m {
			if s == t {
				continue already_seen
			}
		}
		m = append(m, s)
	}
	return m
}

func main() {
	str1 := []string{"a", "b"}
	str2 := []string{"c", "d", "a"}
	fmt.Printf("%#v\n", MergeStringSlices(str1, str2))
}

答案2

得分: 2

从零开始,当你添加一个元素时,长度加一。例如,

package main

import "fmt"

func MergeArrays(a1, a2 []string) []string {
    m := make([]string, 0, len(a1)+len(a2))
    k := make(map[string]bool, len(a1)+len(a2))
    for _, aa := range [][]string{a1, a2} {
        for _, s := range aa {
            if !k[s] {
                m = append(m, s)
                k[s] = true
            }
        }
    }
    return m
}

func main() {
    a1 := []string{"a", "b"}
    a2 := []string{"c", "d", "a"}
    m := MergeArrays(a1, a2)
    fmt.Println(len(m), m)
}

输出结果:

4 [a b c d]
英文:

Start with a length of zero and add one when you append an element. For example,

package main

import "fmt"

func MergeArrays(a1, a2 []string) []string {
	m := make([]string, 0, len(a1)+len(a2))
	k := make(map[string]bool, len(a1)+len(a2))
	for _, aa := range [][]string{a1, a2} {
		for _, s := range aa {
			if !k
展开收缩
{ m = append(m, s) k
展开收缩
= true } } } return m } func main() { a1 := []string{"a", "b"} a2 := []string{"c", "d", "a"} m := MergeArrays(a1, a2) fmt.Println(len(m), m) }

Output:

4 [a b c d]

huangapple
  • 本文由 发表于 2014年10月28日 01:10:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/26592967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定