Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the uniqueness inside a for-loop?

Is there a way to check slices/maps for the presence of a value?

I would like to add a value to a slice only if it does not exist in the slice.

This works, but it seems verbose. Is there a better way to do this?

orgSlice := []int{1, 2, 3} newSlice := []int{} newInt := 2      newSlice = append(newSlice, newInt) for _, v := range orgSlice {     if v != newInt {         newSlice = append(newSlice, v)     } }  newSlice == [2 1 3] 
like image 563
Kyle Finley Avatar asked Feb 12 '12 18:02

Kyle Finley


2 Answers

Your approach would take linear time for each insertion. A better way would be to use a map[int]struct{}. Alternatively, you could also use a map[int]bool or something similar, but the empty struct{} has the advantage that it doesn't occupy any additional space. Therefore map[int]struct{} is a popular choice for a set of integers.

Example:

set := make(map[int]struct{}) set[1] = struct{}{} set[2] = struct{}{} set[1] = struct{}{} // ...  for key := range(set) {   fmt.Println(key) } // each value will be printed only once, in no particular order   // you can use the ,ok idiom to check for existing keys if _, ok := set[1]; ok {   fmt.Println("element found") } else {   fmt.Println("element not found") } 
like image 85
tux21b Avatar answered Sep 21 '22 13:09

tux21b


Most efficient is likely to be iterating over the slice and appending if you don't find it.

func AppendIfMissing(slice []int, i int) []int {     for _, ele := range slice {         if ele == i {             return slice         }     }     return append(slice, i) } 

It's simple and obvious and will be fast for small lists.

Further, it will always be faster than your current map-based solution. The map-based solution iterates over the whole slice no matter what; this solution returns immediately when it finds that the new value is already present. Both solutions compare elements as they iterate. (Each map assignment statement certainly does at least one map key comparison internally.) A map would only be useful if you could maintain it across many insertions. If you rebuild it on every insertion, then all advantage is lost.

If you truly need to efficiently handle large lists, consider maintaining the lists in sorted order. (I suspect the order doesn't matter to you because your first solution appended at the beginning of the list and your latest solution appends at the end.) If you always keep the lists sorted then you you can use the sort.Search function to do efficient binary insertions.

like image 38
Sonia Avatar answered Sep 21 '22 13:09

Sonia