-
Notifications
You must be signed in to change notification settings - Fork 0
/
nonUniqueElements.js
43 lines (33 loc) · 1.09 KB
/
nonUniqueElements.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
/* eslint-disable camelcase */
/*
You are given a non-empty list of integers (X).
For this task, you should return a list consisting of
only the non-unique elements in this list.
To do so you will need to remove all unique elements
(elements which are contained in a given list only once).
When solving this task, do not change the order of the list.
Example:
input (array of integers): [1, 2, 3, 1, 3]
output (iterable of integers): [1, 3, 1, 3]
1 and 3 are non-unique elements.
More examples:
nonUniqueElements([1, 2, 3, 1, 3]) == [1, 3, 1, 3]
nonUniqueElements([1, 2, 3, 4, 5]) == []
nonUniqueElements([5, 5, 5, 5, 5]) == [5, 5, 5, 5, 5]
nonUniqueElements([10, 9, 10, 10, 9, 8]) == [10, 9, 10, 10, 9]
*/
function isNotUnique(item, index, array){
const array_without_current_elem = array.slice()
delete array_without_current_elem[index]
const found = array_without_current_elem.find(a => a === item)
if(found === undefined){
return false
}
return true
}
export function nonUniqueElements(data) {
if(Array.isArray(data)){
return data.filter(isNotUnique)
}
return false
}