Skip to content

Commit c7dc6b7

Browse files
authored
Update 2215-find-the-difference-of-two-arrays.c
1 parent f1394f4 commit c7dc6b7

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

c/2215-find-the-difference-of-two-arrays.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,33 @@ void swap(int *first_value, int *second_value) {
147147
*first_value = *second_value;
148148
*second_value = tmp_value;
149149
}
150+
151+
// Simpler answer with Sets:
152+
153+
int **findDifference(int *nums1, int nums1Size, int *nums2, int nums2Size,
154+
int *returnSize, int **returnColumnSizes) {
155+
int **answer = (int **)malloc(2 * sizeof(int *));
156+
answer[0] = (int *)malloc(nums1Size * sizeof(int));
157+
answer[1] = (int *)malloc(nums2Size * sizeof(int));
158+
159+
*returnSize = 2;
160+
*returnColumnSizes = (int *)calloc(*returnSize, sizeof(int));
161+
int *answerArrayOneSize = &(*returnColumnSizes)[0];
162+
int *answerArrayTwoSize = &(*returnColumnSizes)[1];
163+
164+
int *table = (int *)calloc(2001, sizeof(int));
165+
166+
for (int i = 0; i < nums1Size; i++) {
167+
table[nums1[i] + 1000] = 1;
168+
}
169+
for (int i = 0; i < nums2Size; i++) {
170+
if (table[nums2[i] + 1000] == 0)
171+
answer[1][(*answerArrayTwoSize)++] = nums2[i];
172+
table[nums2[i] + 1000] = -1;
173+
}
174+
175+
for (int i = 0; i < 2001; i++) {
176+
if (table[i] == 1) answer[0][(*answerArrayOneSize)++] = i - 1000;
177+
}
178+
return answer;
179+
}

0 commit comments

Comments
 (0)