-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarch12.js
More file actions
47 lines (39 loc) · 1.21 KB
/
March12.js
File metadata and controls
47 lines (39 loc) · 1.21 KB
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
// Given an array of integers in which two elements appear exactly once and all other elements appear exactly twice, find the two elements that appear only once.
// const { isInteger } = require("core-js/core/number")
// For example, given the array [2, 4, 6, 8, 10, 2, 6, 10], return 4 and 8. The order does not matter.
// Follow-up: Can you do this in linear time and constant space?
const returnUnique = function (myArray) {
// input verification
if(!Array.isArray(myArray)){
console.log("invalid input, must pass an array")
return
}
if(myArray.length <= 0){
console.log("invalid input, array is empty ")
return
}
let counter = {}
for(let index of myArray){
if(!Number.isInteger(index)){
console.log("invalid input, array must only contain integers")
return
}
if(!counter[index]){
counter[index] = 1
} else if(counter[index]){
counter[index]++
}
}
let unique = []
for(let key of Object.keys(counter)){
if(counter[key] === 1){
unique.push(key)
}
}
console.log( unique)
return unique
}
const demoArray = [2, 4, 6, 8, 10, 2, 6, 10]
returnUnique(demoArray) // [4, 8]
returnUnique([]) // error
returnUnique([2,3,4,'4']) // error