-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
problem: new problem solution - 645 . Set Mismatch
- Loading branch information
Showing
3 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Source : https://leetcode.com/problems/set-mismatch/ | ||
// Author : francisco | ||
// Date : 2024-01-22 | ||
|
||
import { findErrorNums } from "./SetMismatch"; | ||
|
||
describe("Set Mismatch", () => { | ||
test("example 1", () => { | ||
const result: number[] = findErrorNums([1, 2, 2, 4]); | ||
|
||
expect(result).toStrictEqual([2, 3]); | ||
}); | ||
|
||
test("example 2", () => { | ||
const result: number[] = findErrorNums([1, 1]); | ||
|
||
expect(result).toStrictEqual([1, 2]); | ||
}); | ||
|
||
test("additional testcase 3", () => { | ||
const result: number[] = findErrorNums([2, 2]); | ||
|
||
expect(result).toStrictEqual([2, 1]); | ||
}); | ||
|
||
test("additional testcase 4", () => { | ||
const result: number[] = findErrorNums([1, 3, 3]); | ||
|
||
expect(result).toStrictEqual([3, 2]); | ||
}); | ||
|
||
test("additional testcase 5", () => { | ||
const result: number[] = findErrorNums([1, 2, 3, 7, 5, 6, 7, 8, 9, 10]); | ||
|
||
expect(result).toStrictEqual([7, 4]); | ||
}); | ||
|
||
test("additional testcase 6", () => { | ||
const result: number[] = findErrorNums([1, 2, 3, 4, 5, 6, 7, 8, 4, 10]); | ||
|
||
expect(result).toStrictEqual([4, 9]); | ||
}); | ||
|
||
test("additional testcase 7", () => { | ||
const result: number[] = findErrorNums([1, 6, 3, 4, 5, 6, 7, 8, 9, 10]); | ||
|
||
expect(result).toStrictEqual([6, 2]); | ||
}); | ||
|
||
test("additional testcase 8", () => { | ||
const result: number[] = findErrorNums([1, 2, 3, 4, 4, 6, 7, 8, 9, 10]); | ||
|
||
expect(result).toStrictEqual([4, 5]); | ||
}); | ||
|
||
test("additional testcase 9", () => { | ||
const result: number[] = findErrorNums([5, 9, 8, 3, 10, 6, 7, 1, 10, 4]); | ||
|
||
expect(result).toStrictEqual([10, 2]); | ||
}); | ||
|
||
test("additional testcase 10", () => { | ||
const result: number[] = findErrorNums([ | ||
541, 610, 723, 542, 531, 883, 106, 553, 373, 239, 95, 766, 694, 434, 40, | ||
587, 271, 884, 243, 658, 237, 166, 275, 539, 232, 769, 141, 225, 999, 374, | ||
808, 327, 244, 7, 383, 416, 762, 51, 754, 676, 675, 945, 624, 670, 828, | ||
982, 845, 588, 858, 535, 66, 835, 738, 233, 144, 24, 1, 227, 34, 971, 785, | ||
509, 540, 501, 779, 503, 822, 126, 861, 195, 763, 267, 394, 612, 212, 690, | ||
491, 984, 263, 819, 264, 363, 52, 641, 142, 768, 255, 400, 699, 955, 221, | ||
125, 998, 42, 331, 185, 224, 205, 447, 854, 11, 952, 68, 405, 28, 250, | ||
477, 789, 48, 782, 885, 948, 121, 559, 249, 631, 634, 330, 242, 337, 579, | ||
556, 623, 562, 8, 622, 338, 439, 716, 442, 809, 710, 510, 47, 189, 801, | ||
506, 892, 911, 973, 687, 775, 795, 563, 625, 109, 701, 339, 375, 345, 37, | ||
487, 460, 26, 113, 802, 83, 632, 86, 829, 135, 677, 790, 402, 470, 719, | ||
572, 384, 678, 32, 602, 278, 959, 450, 56, 709, 644, 155, 329, 18, 97, | ||
458, 311, 241, 718, 665, 197, 907, 179, 821, 882, 816, 705, 421, 435, 567, | ||
62, 437, 867, 328, 598, 629, 304, 753, 325, 283, 981, 843, 611, 533, 253, | ||
794, 388, 940, 923, 120, 589, 261, 992, 806, 757, 781, 888, 756, 262, 583, | ||
607, 231, 976, 436, 321, 379, 19, 173, 996, 668, 77, 886, 89, 471, 381, | ||
904, 995, 206, 27, 833, 975, 299, 317, 114, 430, 947, 13, 767, 362, 820, | ||
902, 664, 191, 744, 761, 200, 478, 178, 803, 913, 251, 773, 396, 100, 978, | ||
879, 792, 516, 772, 245, 693, 71, 813, 659, 504, 613, 112, 199, 815, 932, | ||
390, 590, 215, 909, 692, 536, 685, 990, 147, 839, 96, 444, 547, 571, 298, | ||
455, 21, 944, 620, 614, 453, 266, 787, 377, 759, 965, 272, 492, 866, 475, | ||
395, 353, 755, 75, 585, 827, 258, 122, 118, 897, 268, 688, 724, 87, 807, | ||
140, 584, 650, 804, 192, 903, 315, 226, 397, 138, 107, 217, 94, 269, 176, | ||
565, 165, 88, 413, 758, 733, 415, 341, 956, 846, 566, 682, 336, 369, 99, | ||
855, 285, 812, 184, 628, 655, 524, 626, 617, 717, 830, 643, 412, 780, 853, | ||
167, 193, 171, 473, 214, 901, 347, 457, 496, 389, 399, 10, 680, 78, 391, | ||
288, 935, 417, 257, 356, 575, 158, 512, 282, 194, 604, 320, 850, 9, 737, | ||
525, 403, 180, 45, 577, 69, 284, 14, 987, 874, 174, 930, 857, 967, 355, | ||
916, 880, 348, 798, 235, 953, 865, 899, 4, 303, 739, 484, 615, 143, 365, | ||
378, 934, 674, 350, 527, 732, 333, 561, 871, 43, 954, 422, 463, 582, 91, | ||
825, 672, 479, 361, 133, 797, 908, 983, 31, 385, 295, 555, 182, 300, 57, | ||
869, 58, 482, 715, 132, 514, 778, 621, 119, 409, 498, 515, 519, 560, 401, | ||
752, 695, 760, 832, 530, 111, 154, 340, 175, 223, 468, 922, 408, 707, 788, | ||
910, 502, 218, 359, 924, 586, 162, 704, 117, 65, 962, 550, 234, 912, 481, | ||
105, 793, 432, 286, 115, 662, 876, 499, 254, 451, 805, 393, 896, 41, 59, | ||
601, 480, 708, 219, 443, 163, 656, 918, 60, 280, 751, 230, 969, 307, 551, | ||
207, 735, 684, 683, 198, 927, 591, 334, 146, 936, 190, 633, 20, 938, 743, | ||
101, 201, 796, 346, 915, 714, 467, 152, 312, 6, 570, 116, 204, 671, 863, | ||
136, 791, 667, 508, 276, 537, 989, 964, 452, 160, 92, 72, 36, 50, 483, | ||
545, 966, 495, 721, 84, 654, 731, 411, 859, 287, 494, 849, 33, 406, 98, | ||
428, 630, 23, 292, 177, 354, 834, 102, 980, 534, 29, 279, 811, 459, 720, | ||
358, 569, 928, 696, 856, 921, 522, 386, 639, 997, 521, 746, 814, 35, 906, | ||
81, 30, 900, 472, 847, 352, 61, 895, 890, 63, 260, 236, 335, 22, 486, 488, | ||
925, 464, 367, 203, 202, 770, 852, 507, 636, 917, 108, 130, 661, 729, 745, | ||
573, 349, 774, 557, 985, 929, 157, 319, 616, 474, 593, 229, 170, 55, 449, | ||
740, 220, 993, 293, 943, 404, 595, 454, 93, 838, 669, 49, 660, 46, 979, | ||
568, 505, 960, 213, 12, 448, 891, 425, 970, 642, 1000, 844, 653, 651, 544, | ||
842, 145, 764, 366, 961, 247, 734, 872, 296, 259, 712, 574, 2, 860, 851, | ||
949, 53, 172, 824, 609, 164, 324, 517, 870, 750, 810, 469, 581, 370, 446, | ||
186, 697, 240, 376, 429, 986, 893, 942, 210, 476, 875, 252, 38, 799, 123, | ||
649, 645, 44, 840, 248, 991, 398, 169, 532, 898, 652, 500, 558, 640, 548, | ||
837, 431, 599, 711, 17, 552, 419, 420, 700, 600, 518, 546, 663, 957, 931, | ||
137, 618, 465, 418, 776, 538, 485, 427, 129, 318, 440, 266, 489, 783, 410, | ||
466, 368, 456, 706, 564, 168, 80, 841, 54, 461, 274, 549, 894, 603, 826, | ||
728, 881, 877, 580, 963, 297, 82, 371, 638, 914, 526, 128, 619, 523, 941, | ||
67, 424, 290, 188, 131, 646, 868, 441, 817, 657, 209, 592, 722, 765, 950, | ||
310, 596, 994, 73, 748, 211, 187, 149, 357, 889, 528, 786, 689, 360, 691, | ||
423, 749, 727, 777, 972, 818, 306, 862, 713, 836, 605, 302, 594, 208, 608, | ||
438, 342, 698, 784, 3, 316, 70, 16, 800, 265, 576, 887, 681, 951, 76, 637, | ||
5, 635, 127, 161, 216, 256, 426, 926, 364, 351, 946, 344, 831, 606, 183, | ||
578, 666, 937, 322, 64, 151, 387, 627, 462, 326, 332, 104, 196, 823, 988, | ||
181, 597, 222, 139, 281, 246, 308, 968, 343, 673, 490, 309, 974, 878, 301, | ||
513, 730, 933, 110, 554, 919, 747, 939, 25, 277, 977, 543, 679, 148, 289, | ||
270, 228, 85, 238, 407, 742, 511, 294, 323, 497, 153, 686, 771, 134, 392, | ||
905, 79, 124, 529, 848, 725, 920, 273, 493, 39, 433, 873, 74, 648, 305, | ||
90, 159, 864, 958, 313, 380, 314, 150, 156, 372, 291, 702, 647, 103, 703, | ||
741, 15, 736, 726, 382, 445, 414, | ||
]); | ||
|
||
expect(result).toStrictEqual([266, 520]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Source : https://leetcode.com/problems/set-mismatch/ | ||
// Author : francisco | ||
// Date : 2024-01-22 | ||
|
||
/***************************************************************************************************** | ||
* | ||
* You have a set of integers s, which originally contains all the numbers from 1 to n. Unfortunately, | ||
* due to some error, one of the numbers in s got duplicated to another number in the set, which | ||
* results in repetition of one number and loss of another number. | ||
* | ||
* You are given an integer array nums representing the data status of this set after the error. | ||
* | ||
* Find the number that occurs twice and the number that is missing and return them in the form of an | ||
* array. | ||
* | ||
* Example 1: | ||
* Input: nums = [1,2,2,4] | ||
* Output: [2,3] | ||
* Example 2: | ||
* Input: nums = [1,1] | ||
* Output: [1,2] | ||
* | ||
* Constraints: | ||
* | ||
* 2 <= nums.length <= 10^4 | ||
* 1 <= nums[i] <= 10^4 | ||
******************************************************************************************************/ | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @returns {number[]} | ||
* find the number that occurs twice and the number that is missing and return them in the form of an array | ||
* Tests: | ||
* I: nums = [1,2,2,4] -> O: [2,3] | ||
* I: nums = [1,1] -> O: [1,2] | ||
* Template: | ||
* set for numbers seen so far, seen | ||
* iteration nums - keep a sum accumulator, sum to count the sum of all unique numbers | ||
* sum of an arithmetic progression is: S = n/2 * (a1 + an), where a1 is the first term and an is the last | ||
* by replacing a1 with 1 and an with the length of the array, n, we get: S = (n * (1 + n))/2 | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
export function findErrorNums(nums: number[]): number[] { | ||
const result: number[] = [0, 0]; | ||
|
||
const seen = new Set<number>(); | ||
let sum: number = 0; | ||
|
||
for (const n of nums) { | ||
if (seen.has(n)) result[0] = n; | ||
else { | ||
seen.add(n); | ||
sum += n; | ||
} | ||
} | ||
|
||
result[1] = (nums.length * (1 + nums.length)) / 2 - sum; | ||
|
||
return result; | ||
} |