-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt2.txt
54 lines (38 loc) · 1.17 KB
/
prompt2.txt
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
Write an algorithm that removes duplicates from an array. Do not use a function like filter() to solve this. Once you have solved the problem, demonstrate how it can be solved with filter(). Solve the problem with and without recursion.
Example
Input: [7, 9, "hi", 12, "hi", 7, 53]
Output: [7, 9, "hi", 12, 53]
__________________________________________________________________
input - array (diff data types)
output - array (diff data types)
same order as original?
solve w .filter
solve w.o .filter
.includes
.push []
for loop
WITHOUT RECURSION OR .FILTER:
const removeDuplicates = (inputArray) => {
let resultArray = [];
if (!Array.isArray(resultArray)) {
return "Please enter an array";
} else {
for (let i=0; i < inputArray.length; i++) {
if (!resultArray.includes(inputArray[i])) {
resultArray.push(inputArray[i]);
};
};
};
return resultArray;
};
inputArray = [7, 9, "hi", 7, 12, "hi", 53];
result = [7, 9, "hi", 12, 53];
WITH RECURSION:
const removeDuplicates = (array) => {
};
USING FILTER:
const removeDuplicates = (array) => {
return unique = array.filter((item, index) => {
return array.indexOf(item) === index;
});
};