Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Write a function which will take an array as a parameter and will return only the numbers that are repeated once.
- // Eg: sample array [1, 2, 2, -4, 3, 1, -3, 4, 4, 1, 5, -4],
- // output: -3, 3, 5
- const array = [1, 2, 2, -4, 3, 1, -3, 4, 4, 1, 5, -4]
- const max = Math.max(...array)
- const min = Math.min(...array)
- const positive_counts = []
- const negative_counts = []
- const answers = []
- for (let i = 0; i <= max; i++) {
- positive_counts[i] = 0
- }
- for (let i = 0; i <= (-1 * min); i++) {
- negative_counts[i] = 0
- }
- for (let i = 0; i < array.length; i++) {
- if (array[i] >= 0) {
- positive_counts[array[i]]++
- } else {
- negative_counts[(-1 * array[i])]++
- }
- }
- for (let i = 0; i < negative_counts.length; i++) {
- if (negative_counts[i] == 1) {
- answers.push((-1 * i))
- }
- }
- for (let i = 0; i < positive_counts.length; i++) {
- if (positive_counts[i] == 1) {
- answers.push(i)
- }
- }
- console.log(answers)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement