-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelping-other-user_array_strip_unique.js
127 lines (95 loc) · 3.89 KB
/
helping-other-user_array_strip_unique.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* Given an integer array nums sorted in non-decreasing order,
reemove the duplicates in-place such that each unique
element appears only once. The relative order of the elements
should be kept the same.
Since it is impossible to change the length of the array in some
languages, you must instead have the result be placed in
the first part of the array nums. More formally, if there are k
elements after removing the duplicates, then the first k
elements of nums should hold the final result. It does not matter
what you leave beyond the first k elements.
Return k after placing the final result in the first k slots of
nums.
Do not allocate extra space for another array. You must do this by
modifying the input array in-place with O(1) extra memory.
Custom Judge:
The judge will test your solution with the following code: */
/* int[] nums = [...]; // Input array
int[] expectedNums = [...]; // The expected answer with correct length
int k = removeDuplicates(nums); // Calls your implementation
assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
assert nums[i] == expectedNums[i];
}
If all assertions pass, then your solution will be accepted.
Example 1:
Input: nums = [1,1,2]
Output: 2, nums = [1,2,]
Explanation: Your function should return k = 2, with the first two elements of nums being
1 and 2 respectively. It does not matter what you leave beyond the returned k (hence they
are underscores). */
//
//
const numarray = [1,1,2,2,3,4,5,6,7,7,8,8,9,10,11,11,11,12]; // test input array
/*var sliceSwap = function(nums, start, end) {
// write function to shallow copy the array back into ascending order with NULL values
// appended to the end of the array
// call function from removeDupplicates() function whenever spaces are created() {
//code goes here
return numarray.slice(0, start) + numarrnumarray.slice(start, end);
},*/
var moveArrayChunk = function(arrN, start, remove, addons) {
/* write function to use copyWithin to do an in-memory copy of chunks of the array
to fill in gaps or blank array elements.
code a badass function
PARAMETERS: Usage and examples
param nums - original array of number
param start [optional] - zero-based index to start copying elements from.
if negative, start will count backwards from the
end of the array.'
**If start is ommitted, copy will start from index 0.**
param end [optional] - zero-based index to end copying elements from. copyWithin
copies up to but not including the end. If negative, end
will be counted backwards from end of the array.
**If end is ommitted, copy will end at last index of array.**
*/
// my code
return (nums.slice());
};
var removeDuplicates = function(nums) {
var count = 0;
/* for (var i = 0; i < nums.length; i++){
if (nums[i + 1] === nums[i]) nums[i] = "";
}
for(var x = 0; x < nums.length; x++){
if (nums[x] === ''){
for (j = x; j < nums.length - x - 1; j++){
temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
}
}
}
var y = 0;
while(nums[y] != ""){
count++;
y++;
} */
//my way of filtering O(1) memory footprint
var arrLen = nums.length;
for (var x = 0; x < arrLen; x++) {
if (x != '' && x != nums.lastIndexOf(nums[x])){
removeSteps = nums.lastIndexOf(nums[x]) - x;
while (removeSteps > 0) {
nums[x + removeSteps] = "";
removeSteps--;
}
moveArrayChunk(nums, x, nums.lastIndexOf(nums[x]))
}
}
console.log(count, nums);
return (count, nums);
};
console.log(numarray, "nums array");
var response = removeDuplicates(numarray);
console.log(response);