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>
This commit is contained in:
Matej Focko 2025-01-14 17:49:10 +01:00
parent f357df8fd6
commit 5af8d632b2
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -5,16 +5,12 @@ public class Solution {
var (common, result) = (0, new int[n]);
for (var i = 0; i < n; ++i) {
++freqs[xs[i]];
if (freqs[xs[i]] == 2) {
// occurred in both
++common;
}
++freqs[ys[i]];
if (freqs[ys[i]] == 2) {
// occurred in both
++common;
foreach (var x in new List<int>() { xs[i], ys[i] }) {
++freqs[x];
if (freqs[x] == 2) {
// occurred in both
++common;
}
}
result[i] = common;