-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercises_chapter8.js
85 lines (74 loc) · 1.57 KB
/
exercises_chapter8.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
let array1 = [1, 2, 3, 4, 5];
let array2 = [0, 2, 4, 6, 8];
function Intersection(array1, array2)
{
let result = [];
let hashArray1 = {};
let hashArray2 = {};
let arrayPeque = [];
//inicialmente no se supone que tienen el mismo tamaño
arrayPeque = array1;
if(array1.length > array2.length)
{
arrayPeque = array2;
}
else if(array1.length < array2.length)
{
arrayPeque = array1;
}
for(let item of array1)
{
hashArray1[item] = true;
}
for(let item of array2)
{
hashArray2[item] = true;
}
for(let item of arrayPeque)
{
if(hashArray1[item] && hashArray2[item])
result.push(item);
}
return result;
}
console.log(Intersection(array2, array1));
console.log('-------------------------------------')
function ReturnFirstDuplicated(stringArray)
{
let hashArray = {};
for(const item of stringArray)
{
if(hashArray[item] === undefined)
hashArray[item] = true;
else
return 'item: ' + item + ' is duplicated'
}
console.log(hashArray);
}
console.log(ReturnFirstDuplicated(["a", "b", "c", "d", "c", "e", "f"]));
console.log('-----------------------------------');
function FirstNonDuplicated (somePhrase)
{
let hashPhrase = {};
let hashPhrasecount = {};
for(const item of somePhrase)
{
if(hashPhrase[item] === undefined)
{
hashPhrase[item] = true;
hashPhrasecount[item] = 1;
}
else
{
hashPhrasecount[item] ++;
}
}
for(let i in hashPhrasecount)
{
if(hashPhrasecount[i] === 1)
{
return i;
}
}
}
console.log(FirstNonDuplicated('minimum'));