Home > database >  Remove duplicate from response
Remove duplicate from response

Time:08-30

I have the following task: I'm making loop that doing requests to server and recieving a value, for example it is a slice of ints. Then I need to pass this slice of ints to another task. The thing is that sometime I recieve duplicate value from server, for example:

resp1 := []int{1,2,3,4} > do some things
resp2 := []int{3,4,5,6} > do some things

But I want to pass only NEW values from response to next task

resp1 := []int{1,2,3,4} > pass 1,2,3,4
resp2 := []int{3,4,5,6} > pass 5,6

Is there any solution to avoid / "clean up" duplicates from value?

CodePudding user response:

The general idea would be to maintain a global slice map (as suggested by mkopriva in the comments) which would memorize any value you are passing to your other task.

Build also a new slice which would:

  • test for each value 'x' (of respx) in the global map:
    • if present, your new slice would not include that value
    • if absent, your new slice would include that value (and an empty struct value would be added to your global map, with 'x' as key)
  • pass the new slice (with only new values, never passed before) to the new task

The point is: your current task is stateful and need to maintain a state between each call.

You will need some kind of global variable and/or singleton, something which will span the life of the execution of your program.
mkopriva also mentions the "visited pattern" as implementation examples.

CodePudding user response:

You can store the values you have already seen in a map and using that you can generate unique values each time you get a response. A function to get unique response can be like this :

func getUniqueResponse(curResp []int, seen map[int]bool) []int {
    var resp []int
    for _, num := range curResp {
        if !seen[num] {
            seen[num] = true
            resp = append(resp, num)
        }
    }
    return resp
}

You can declare a map[int]bool and pass that map along with slice and receive a unique slice.

seen := make(map[int]bool)
resp := []int{1, 2, 3, 4}
uniqueResp := getUniqueResponse(resp, seen)
// Do other work with unique Response
resp = []int{3, 4, 5, 6}
uniqueResp = getUniqueResponse(resp, seen)
// Do other work with unique Response
  •  Tags:  
  • go
  • Related