-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountUniqueValues.js
62 lines (50 loc) · 1.33 KB
/
countUniqueValues.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function countUniqueValues(values) {
let count = 0;
for (value in values) {
if (
values[value] != values[value - 1] &&
values[value] != values[value + 1]
) {
count++;
}
}
return count;
}
// console.log(countUniqueValues([])); // 0
// console.log(countUniqueValues([1])); // 1
// console.log(countUniqueValues([1, 1, 2, 3, 4])); //4
// console.log(countUniqueValues([-2, -1, -1, 0, 1])); //4
// can we alter array? if so, let's use pointers!
// function countUniqueValuesWithPointer(values) {
// let j = 0;
// if (values.length === 0) {
// return 0;
// }
// for (let value in values) {
// if (values[value] == values[j]) {
// values[j] = values[value];
// } else {
// j++;
// values[j] = values[value];
// }
// }
// return j + 1;
// }
function countUniqueValuesWithPointer(array) {
if (!array || array.length === 0) {
return 0;
}
let pointer = 0;
for (let number of array) {
if (number !== array[pointer]) {
pointer++;
array[pointer] = number;
}
}
return pointer + 1;
}
console.log(countUniqueValuesWithPointer([])); // 0
console.log(countUniqueValuesWithPointer([1])); // 1
// [1,2,3, 4, xxxx ] //3
console.log(countUniqueValuesWithPointer([1, 1, 2, 3, 4])); //4
console.log(countUniqueValuesWithPointer([-2, -1, -1, 0, 1])); //4