doumu5023 2014-04-24 17:22
浏览 46
已采纳

我们可以在go中编写通用数组/切片重复数据删除吗?

Is there a way to write a generic array/slice deduplication in go, for []int we can have something like (from http://rosettacode.org/wiki/Remove_duplicate_elements#Go ):

func uniq(list []int) []int {
  unique_set := make(map[int] bool, len(list))
  for _, x := range list {
     unique_set[x] = true
   }
  result := make([]int, len(unique_set))
  i := 0
  for x := range unique_set {
     result[i] = x
    i++
  }
  return result
}

But is there a way to extend it to support any array? with a signature like:

func deduplicate(a []interface{}) []interface{}

I know that you can write that function with that signature, but then you can't actually use it on []int, you need to create a []interface{} put everything from the []int into it, pass it to the function then get it back and put it into a []interface{} and go through this new array and put everything in a new []int.

My question is, is there a better way to do this?

  • 写回答

3条回答 默认 最新

  • dougu1952 2014-04-24 22:56
    关注

    While VonC's answer probably does the closest to what you really want, the only real way to do it in native Go without gen is to define an interface

    type IDList interface {
       // Returns the id of the element at i
       ID(i int) int
    
       // Returns the element
       // with the given id
       GetByID(id int) interface{}
    
       Len() int
    
       // Adds the element to the list
       Insert(interface{})
    }
    
    // Puts the deduplicated list in dst
    func Deduplicate(dst, list IDList) {
        intList := make([]int, list.Len())
        for i := range intList {
            intList[i] = list.ID(i)
        }
    
        uniques := uniq(intList)
        for _,el := range uniques {
            dst.Insert(list.GetByID(el))
        }
    }
    

    Where uniq is the function from your OP.

    This is just one possible example, and there are probably much better ones, but in general mapping each element to a unique "==able" ID and either constructing a new list or culling based on the deduplication of the IDs is probably the most intuitive way.

    An alternate solution is to take in an []IDer where the IDer interface is just ID() int. However, that means that user code has to create the []IDer list and copy all the elements into that list, which is a bit ugly. It's cleaner for the user to wrap the list as an ID list rather than copy, but it's a similar amount of work either way.

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料