常用类型及数据操作方法
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
863 B

  1. /*
  2. * string functions
  3. */
  4. package helper
  5. // 求差集
  6. func Diff(slice1, slice2 []string) []string {
  7. m := make(map[string]int)
  8. nn := make([]string, 0)
  9. inter := Intersect(slice1, slice2)
  10. for _, v := range inter {
  11. m[v]++
  12. }
  13. for _, value := range slice1 {
  14. times, _ := m[value]
  15. if times == 0 {
  16. nn = append(nn, value)
  17. }
  18. }
  19. return nn
  20. }
  21. // 求交集
  22. func Intersect(slice1, slice2 []string) []string {
  23. m := make(map[string]int)
  24. nn := make([]string, 0)
  25. for _, v := range slice1 {
  26. m[v]++
  27. }
  28. for _, v := range slice2 {
  29. times, _ := m[v]
  30. if times == 1 {
  31. nn = append(nn, v)
  32. }
  33. }
  34. return nn
  35. }
  36. // 求并集
  37. func Union(slice1, slice2 []string) []string {
  38. m := make(map[string]int)
  39. for _, v := range slice1 {
  40. m[v]++
  41. }
  42. for _, v := range slice2 {
  43. times, _ := m[v]
  44. if times == 0 {
  45. slice1 = append(slice1, v)
  46. }
  47. }
  48. return slice1
  49. }