Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 189
Good morning! Here's your coding interview problem for today.

This problem was asked by Google.

Given an array of elements, return the length of the longest subarray where all its elements are distinct.

For example, given the array `[5, 1, 3, 5, 2, 3, 4, 1]`, return `5` as the longest subarray of distinct elements is `[5, 2, 3, 4, 1]`.

Pull request description:
Day189
Resolves #391.

Patch:
diff --git a/README.md b/README.md
index 007c4ff..8064228 100644
--- a/README.md
+++ b/README.md
@@ -199,3 +199,4 @@ problems from
 * [Day 186](https://github.com/vaskoz/dailycodingproblem-go/issues/382)
 * [Day 187](https://github.com/vaskoz/dailycodingproblem-go/issues/387)
 * [Day 188](https://github.com/vaskoz/dailycodingproblem-go/issues/388)
+* [Day 189](https://github.com/vaskoz/dailycodingproblem-go/issues/391)
diff --git a/day189/problem.go b/day189/problem.go
new file mode 100644
index 0000000..a317bc4
--- /dev/null
+++ b/day189/problem.go
@@ -0,0 +1,27 @@
+package day189
+
+// LengthLongestDistinct returns the length of the longest
+// sub-array where all elements are distinct.
+// Runs in O(N) time and O(N) extra space.
+func LengthLongestDistinct(nums []int) int {
+	valueToPos := make(map[int]int)
+	var longest int
+	for i, v := range nums {
+		if pos, found := valueToPos[v]; found {
+			if l := len(valueToPos); l > longest {
+				longest = l
+			}
+			for val, p := range valueToPos {
+				if p <= pos {
+					delete(valueToPos, val)
+				}
+			}
+		}
+		valueToPos[v] = i
+
+	}
+	if l := len(valueToPos); l > longest {
+		longest = l
+	}
+	return longest
+}


Test patch:
diff --git a/day189/problem_test.go b/day189/problem_test.go
new file mode 100644
index 0000000..9715df1
--- /dev/null
+++ b/day189/problem_test.go
@@ -0,0 +1,27 @@
+package day189
+
+import "testing"
+
+var testcases = []struct {
+	nums   []int
+	length int
+}{
+	{[]int{5, 1, 3, 5, 2, 3, 4, 1}, 5},
+}
+
+func TestLengthLongestDistinct(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := LengthLongestDistinct(tc.nums); result != tc.length {
+			t.Errorf("Expected %v, got %v", tc.length, result)
+		}
+	}
+}
+
+func BenchmarkLengthLongestDistinct(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			LengthLongestDistinct(tc.nums)
+		}
+	}
+}