douzhanglu4591 2019-08-29 09:09
浏览 2
已采纳

重复数据删除结构数组

I have an array of struct and I want to remove all the duplicates element but keep the last element in the array. Something similar to hashmap where I can update the last struct matched every time to the new array

I have a struct something like this

type samplestruct struct {
    value1           string    
    value2           string   
    value3           string   
    value4           string
    value5           string
}

In my array of struct if value1, value2 and value3 of any struct is same , remove all the duplicates and keep the last struct.

func unique(sample []samplestruct) []samplestruct {
    var unique []samplestruct

    for _, v := range sample {
        skip := false
        for _, u := range unique {
            if v.value1 == u.value1 && v.value2 == u.value2 && v.value3 == u.value3 {
                skip = true
                break
            }
        }
        if !skip {
            unique = append(unique, v)
        }
    }
    return unique

}

This code return me the first struct that matched the condition provided but I want the last struct that matches the condition

Given Input -

[
samplestruct{"ram","rahim","india","34","india"},
samplestruct{"ram","rahim","india","38","America"},
samplestruct{"ram","rahim","india","40","Jamica"},
samplestruct{"amit","rawat","bangladesh","35","hawai"},
samplestruct{"amit","rawat","bangladesh","36","india"}
]

ExpectedOutput -

[
samplestruct{"ram","rahim","india","40","Jamica"},
samplestruct{"amit","rawat","bangladesh","36","india"}
]
  • 写回答

3条回答 默认 最新

  • douaoli5328 2019-08-29 11:11
    关注

    Nice little exercise, here is one solution which I will explain below:

    package main
    
    import "fmt"
    
    func main() {
        all := []person{
            {"ram", "rahim", "india", "34", "india"},
            {"ram", "rahim", "india", "38", "America"},
            {"ram", "rahim", "india", "40", "Jamica"},
            {"amit", "rawat", "bangladesh", "35", "hawai"},
            {"amit", "rawat", "bangladesh", "36", "india"},
        }
    
        var deduped []person
        // add the last occurrence always
        for i := len(all) - 1; i >= 0; i-- {
            if !contains(deduped, all[i]) {
                deduped = append([]person{all[i]}, deduped...)
            }
        }
    
        for _, x := range deduped {
            fmt.Println(x)
        }
    }
    
    type person [5]string
    
    func eq(a, b person) bool {
        return a[0] == b[0] && a[0] == b[0] && a[0] == b[0]
    }
    
    func contains(list []person, x person) bool {
        for i := range list {
            if eq(x, list[i]) {
                return true
            }
        }
        return false
    }
    

    We step through the input array backwards in order to catch the last of multiple equal items. Then we want to append that item to the back of the deduped array. That is why we revert the append operation, creating a new temporary one-item person slice and append the previous to it.

    Efficiency-wise, this solution has some drawbacks, appending to the one-item slice will use O(n²) space as it procudes a new slice every time, pre-allocating an array of len(all), appending to and and reversing it afterwards would solve that problem.

    The second performance issue that might arise if you do this for a zillion persons is the contains function which is O(n²) lookups for the program. This could be solved with a map[person]bool.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?