-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
35 lines (28 loc) · 849 Bytes
/
index.ts
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
/***
Returns the first repeated item from an array if any
@throws {Error} if there is no item repetition
*/
function repeatedItemLoops<T>(array: T[]): T {
for (let i = 0; i < array.length; i++) {
for (let j = i + 1; j < array.length; j++) {
if (array[i] === array[j]) return array[i];
}
}
throw new Error('No item repetition');
}
function repeatedItem<T>(array: T[]): T {
const set = new Set<T>();
for (const item of array) {
if (set.has(item)) return item;
else set.add(item);
}
// throw new Error('No item repetition');
// converted for plunker output
// return Error('No item repetition');
console.log('No item repetition');
}
const itemRepetition = [1, 3, 5, 9, 1]
const noItemRepetition = [1, 3, 5, 9]
console.log(repeatedItem(itemRepetition));
console.log(repeatedItem(noItemRepetition));