Skip to content

Commit 221b9cd

Browse files
committed
feat: add unique array functions
1 parent 0c96f83 commit 221b9cd

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

basic_sorting/selection_sort.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: Chacha
33
* @Date: 2018-12-20 22:00:30
44
* @Last Modified by: Chacha
5-
* @Last Modified time: 2022-04-04 16:38:24
5+
* @Last Modified time: 2022-05-08 10:52:28
66
*/
77

88
/**
@@ -24,8 +24,8 @@
2424
#include <iostream>
2525
#include <vector>
2626
using namespace std;
27-
template <typename T> //整数或浮点数皆可使用,若要使用类(class)或结构体(struct)时必须重载大于(>)运算符
2827

28+
template <typename T> // 整数或浮点数皆可使用,若要使用类(class)或结构体(struct)时必须重载大于(>)运算符
2929
void selectionSort(vector<T> &arr)
3030
{
3131
int len = arr.size();

js/unique_array.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @Author: Chacha
3+
* @Date: 2022-05-08 11:30:09
4+
* @Last Modified by: Chacha
5+
* @Last Modified time: 2022-05-08 15:46:10
6+
*/
7+
8+
// 1. 最简短方便的方式
9+
const uniqueArray1 = (array) => {
10+
return [...new Set(array)];
11+
};
12+
13+
// 2. indexOf去重
14+
const uniqueArray2 = (array) => {
15+
let result = [];
16+
17+
array.forEach((it, i) => {
18+
if (result.indexOf(it) === -1) {
19+
result.push(it);
20+
}
21+
});
22+
23+
return result;
24+
};
25+
26+
// 3. indexOf去重另一个版本
27+
const uniqueArray3 = (array) => {
28+
return array.filter((it, i) => array.indexOf(it) === i);
29+
};
30+
31+
// 4. Array.from去重
32+
const uniqueArray4 = (array) => {
33+
return Array.from(new Set(array));
34+
};
35+
36+
let testArray = [1, 2, 3, 1, 2, 3, 4];
37+
38+
console.log(uniqueArray1(testArray));
39+
console.log(uniqueArray2(testArray));
40+
console.log(uniqueArray3(testArray));
41+
console.log(uniqueArray4(testArray));

leetcode/array/find_pivot_index.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: Chacha
33
* @Date: 2018-12-28 18:05:11
44
* @Last Modified by: Chacha
5-
* @Last Modified time: 2021-04-19 22:59:03
5+
* @Last Modified time: 2022-05-08 10:57:34
66
*/
77

88
/**
@@ -33,8 +33,8 @@ using namespace std;
3333

3434
/**
3535
* 题解:
36-
* 计算出全部数组的和,当遍历到第i个元素时,设其左侧元素之和为sum,则其右侧元素之和为 total - numsi - sum。
37-
* 左右侧元素相等即为 sum = total - numsi - sum,即 2 x sum + numsi = total。
36+
* 计算出全部数组的和,当遍历到第i个元素时,设其左侧元素之和为sum,则其右侧元素之和为 total - nums[i] - sum。
37+
* 左右侧元素相等即为 sum = total - nums[i] - sum,即 2 x sum + nums[i] = total。
3838
*
3939
* 当中心索引左侧或右侧没有元素时,即为零个项相加,这在数学上称作「空和」(empty sum)。在程序设计中我们约定「空和是零」。
4040
*

0 commit comments

Comments
 (0)