Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Jan 14, 2025
1 parent 795ec4d commit cfb087f
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions leetcode/daily/2657/sol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/

package main

import "fmt"

func findThePrefixCommonArray(A []int, B []int) []int {
seen := make(map[int]bool) // To track elements seen so far
result := make([]int, len(A))
commonCount := 0

for i := 0; i < len(A); i++ {
if seen[A[i]] {
commonCount++
} else {
seen[A[i]] = true
}

if seen[B[i]] {
commonCount++
} else {
seen[B[i]] = true
}

result[i] = commonCount
}

return result
}

func main() {
A := []int{1, 3, 2, 4}
B := []int{3, 1, 2, 4}
fmt.Println(findThePrefixCommonArray(A, B))
}

0 comments on commit cfb087f

Please sign in to comment.