1
1
package commonCLI
2
2
3
- import "strings"
3
+ import (
4
+ "sort"
5
+ "strconv"
6
+ "strings"
7
+ )
4
8
5
- // StringToStringsArr transform string of comma seperated word to array of words
9
+ // StringToStringsArr transform string of comma seperated words to array of strings
6
10
func StringToStringsArr (str string ) (strArr []string ) {
7
11
for _ , s := range strings .Split (str , "," ) {
8
12
strArr = append (strArr , s )
9
13
}
10
14
return strArr
11
15
}
12
16
17
+ // StringToIntArr transform string of comma seperated words to array of ints
18
+ func StringToIntArr (str string ) (intArr []int ) {
19
+ for _ , s := range strings .Split (str , "," ) {
20
+ num , _ := strconv .Atoi (s )
21
+ intArr = append (intArr , num )
22
+ }
23
+
24
+ sort .Ints (intArr )
25
+ return intArr
26
+ }
27
+
13
28
// StringsSlicesDifference give you diff between 2 strings slices
14
29
func StringsSlicesDifference (slice1 []string , slice2 []string ) []string {
15
30
var diff []string
@@ -29,3 +44,72 @@ func StringsSlicesDifference(slice1 []string, slice2 []string) []string {
29
44
30
45
return diff
31
46
}
47
+
48
+ // IsStringInSlice returns TRUE is slice contains string and false if not
49
+ func IsStringInSlice (a string , list []string ) bool {
50
+ // We need that to not filter for empty list
51
+ if len (list ) > 0 {
52
+ for _ , b := range list {
53
+ if b == a {
54
+ return true
55
+ }
56
+ }
57
+ return false
58
+ }
59
+ return true
60
+ }
61
+
62
+ // RemoveIntDuplicates removes duplicated elements from int array
63
+ func RemoveIntDuplicates (elements []int ) []int {
64
+ // Use map to record duplicates as we find them.
65
+ encountered := map [int ]bool {}
66
+ result := []int {}
67
+
68
+ for v := range elements {
69
+ if encountered [elements [v ]] == true {
70
+ // Do not add duplicate.
71
+ } else {
72
+ // Record this element as an encountered element.
73
+ encountered [elements [v ]] = true
74
+ // Append to result slice.
75
+ result = append (result , elements [v ])
76
+ }
77
+ }
78
+ // Return the new slice.
79
+ return result
80
+ }
81
+
82
+ // RemoveStringDuplicates removes duplicated elements from strings array
83
+ func RemoveStringDuplicates (str []string ) []string {
84
+ // Use map to record duplicates as we find them.
85
+ encountered := map [string ]bool {}
86
+ result := []string {}
87
+
88
+ for v := range str {
89
+ if encountered [str [v ]] == true {
90
+ // Do not add duplicate.
91
+ } else {
92
+ // Record this element as an encountered element.
93
+ encountered [str [v ]] = true
94
+ // Append to result slice.
95
+ if str [v ] != "" {
96
+ result = append (result , str [v ])
97
+ }
98
+ }
99
+ }
100
+ // Return the new slice.
101
+ return result
102
+ }
103
+
104
+ // DeleteSlicefromSlice deletes one slice from another and removes duplicate objects
105
+ func DeleteSlicefromSlice (slice , delete []int ) []int {
106
+ for _ , d := range delete {
107
+ for i := len (slice ) - 1 ; i >= 0 ; i -- {
108
+ if slice [i ] == d {
109
+ slice = append (slice [:i ], slice [i + 1 :]... )
110
+ }
111
+ }
112
+ }
113
+
114
+ return RemoveIntDuplicates (slice )
115
+ }
0 commit comments