Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/simulation/fixtures/edit/3575.ts
13399 views
1
/**
2
* Returns the intersection of two arrays as a new array.
3
* The intersection includes the elements that are common to both input arrays.
4
* The function avoids duplicates by deleting each found element from the set.
5
*
6
* @param arr1 - The first input array.
7
* @param arr2 - The second input array.
8
* @returns A new array containing the intersection of the input arrays.
9
*/
10
export function arrayIntersection(arr1: number[], arr2: number[]): number[] {
11
const set = new Set(arr1);
12
const result: number[] = [];
13
for (let num of arr2) {
14
if (set.has(num)) {
15
result.push(num);
16
set.delete(num); // Avoid duplicates
17
}
18
}
19
return result;
20
}
21
22
/**
23
* This function finds and logs all pairs of numbers from two arrays that add up to a given sum.
24
*
25
* @param arr1 - The first array of numbers.
26
* @param arr2 - The second array of numbers.
27
* @param sum - The target sum. If not provided, defaults to 10.
28
*/
29
export function findPairs(arr1: number[], arr2: number[], sum: number = 10): void {
30
for (let i = 0; i < arr1.length; i++) {
31
for (let j = 0; j < arr2.length; j++) {
32
if (arr1[i] + arr2[j] === sum) {
33
console.log(`Pair: (${arr1[i]}, ${arr2[j]})`);
34
}
35
}
36
}
37
}
38
39
/**
40
* Sorts an array of numbers in ascending order using the Bubble Sort algorithm.
41
*
42
* @param arr - The array of numbers to be sorted.
43
* @returns The sorted array of numbers.
44
*/
45
export function sort(arr: number[]): number[] {
46
let len = arr.length;
47
for (let i = 0; i < len; i++) {
48
for (let j = 0; j < len - i - 1; j++) {
49
if (arr[j] > arr[j + 1]) {
50
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
51
}
52
}
53
}
54
return arr;
55
}
56