slice_utils.go 302 B

123456789101112131415
  1. package silce_utils
  2. // RemoveDuplicates 去除整数切片中的重复元素
  3. func RemoveDuplicates(nums []int) []int {
  4. seen := make(map[int]bool)
  5. var result []int
  6. for _, num := range nums {
  7. if _, ok := seen[num]; !ok {
  8. seen[num] = true
  9. result = append(result, num)
  10. }
  11. }
  12. return result
  13. }