Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions MaxCounters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,20 @@
function solution(N, A) {
// write your code in JavaScript (Node.js 8.9.4)


let max = 0;
let last = 0;
let counters = Array(N).fill(max);

A.forEach((a, index) => {

if (a >= 1 && a <= N ) {
counters[a - 1] += 1;
max = Math.max(max, counters[a - 1]);
} else if (N + 1 == a && last != (N+1)) {
counters.fill(max)
}
if (a >= 1 && a <= N ) {
counters[a - 1] += 1;
max = Math.max(max, counters[a - 1]);
} else if (N + 1 == a && last != (N+1)) {
counters.fill(max)
}

last = a
last = a

});

Expand Down
33 changes: 33 additions & 0 deletions MissingInteger/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Write a function:

// function solution(A);

// that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.

// For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.

// Given A = [1, 2, 3], the function should return 4.

// Given A = [−1, −3], the function should return 1.

// Write an efficient algorithm for the following assumptions:

// N is an integer within the range [1..100,000];
// each element of array A is an integer within the range [−1,000,000..1,000,000].

function solution(A) {
const dictionary = {}
let minimum = 1

A.forEach((element) => {
dictionary[element] = true
if (element === minimum) {
while (dictionary[minimum]) {
minimum++
}
}
})

return minimum;

}