LeetCode/cs/find-the-prefix-common-array-of-two-arrays.cs
Matej Focko 5af8d632b2
cs: add «2657. Find the Prefix Common Array of Two Arrays»
Use ‹foreach› loop instead of a copy-pasta for checking both arrays at
the same time.

URL:	https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/
Signed-off-by: Matej Focko <me@mfocko.xyz>
2025-01-14 17:49:30 +01:00

21 lines
546 B
C#

public class Solution {
public int[] FindThePrefixCommonArray(int[] xs, int[] ys) {
var n = xs.Length;
var freqs = new int[n + 1];
var (common, result) = (0, new int[n]);
for (var i = 0; i < n; ++i) {
foreach (var x in new List<int>() { xs[i], ys[i] }) {
++freqs[x];
if (freqs[x] == 2) {
// occurred in both
++common;
}
}
result[i] = common;
}
return result;
}
}