Advertisement
1HazingAU

Simple remove duplicates

May 30th, 2024
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.44 KB | None | 0 0
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. func Solve(slice []int) []int {
  6.     lastIndex := make(map[int]int)
  7.  
  8.    
  9.     for i, key := range slice {
  10.         lastIndex[key] = i
  11.     }
  12.  
  13.     result := []int{}
  14.  
  15.    
  16.     for i := len(slice) - 1; i >= 0; i-- {
  17.         value := slice[i]
  18.        
  19.         if lastIndex[value] == i {
  20.  
  21.             result = append([]int{value}, result...)
  22.         }
  23.     }
  24.  
  25.     return result
  26. }
  27.  
  28. func main() {
  29.     numbers := []int{1, 1, 4, 5, 1, 2, 1}
  30.  
  31.     fmt.Print(Solve(numbers))
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement