Skip to content

Commit

Permalink
171st Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Jul 20, 2024
1 parent 8d6cb96 commit 6740357
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/page-24/2540. Minimum Common Value/getCommon.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { getCommon } from './getCommon';

describe('2540. Minimum Common Value', () => {
test('getCommon', () => {
{
const nums1 = [1, 2, 3];
const nums2 = [2, 4];
expect(getCommon(nums1, nums2)).toBe(2);
}

{
const nums1 = [1, 2, 3, 6];
const nums2 = [2, 3, 4, 5];
expect(getCommon(nums1, nums2)).toBe(2);
}
});
});
23 changes: 23 additions & 0 deletions src/page-24/2540. Minimum Common Value/getCommon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
type getCommon = (nums1: number[], nums2: number[]) => number;

/**
* Accepted
*/
export const getCommon: getCommon = (nums1, nums2) => {
let pointer1 = 0;
let pointer2 = 0;

while (pointer1 < nums1.length && pointer2 < nums2.length) {
if (nums1[pointer1] === nums2[pointer2]) {
return nums1[pointer1];
}

if (nums1[pointer1] < nums2[pointer2]) {
pointer1 += 1;
} else {
pointer2 += 1;
}
}

return -1;
};

0 comments on commit 6740357

Please sign in to comment.