Sobes.tech
Junior

How would you remove duplicates from an array/slice without using external libraries?

sobes.tech AI

Answer from AI

To remove duplicates from an array or slice without using external libraries, you can use a data structure that stores already encountered elements, such as a map in Go or an object in JavaScript.

Example in Go:

func removeDuplicates(slice []int) []int {
    seen := make(map[int]bool)
    result := []int{}
    for _, v := range slice {
        if !seen[v] {
            seen[v] = true
            result = append(result, v)
        }
    }
    return result
}

The idea is to iterate over the original array, check if each element has been seen before, and if not, add it to the result. This is efficient and does not require third-party libraries.