-
Notifications
You must be signed in to change notification settings - Fork 4
/
All Twice Except One II.js
52 lines (46 loc) · 1.26 KB
/
All Twice Except One II.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
44
45
46
47
48
49
50
51
52
//You are given a number N, and an array such that the array contains all the numbers from 1 to N including N twice,
//except for one, which is present only once
//You have to find the number which is present only once, in the array
function runProgram(input) {
var input = input.split("\n");
var t = +input[0];
var line = 1;
for (var i = 0; i < t; i++) {
var N = +input[line];
line++;
var arr = input[line].trim().split(" ").map(Number);
line++;
solve(N, arr);
}
}
function solve(N, arr) {
arr.sort(function (a, b) { return a - b });
var i = 0;
while (i < 2 * N - 1) {
if (arr[i] != arr[i + 1]) {
console.log(arr[i]);
break;
}
i += 2;
}
}
if (process.env.USER === "") {
runProgram(``);
} else {
process.stdin.resume();
process.stdin.setEncoding("ascii");
let read = "";
process.stdin.on("data", function (input) {
read += input;
});
process.stdin.on("end", function () {
read = read.replace(/\n$/, "");
read = read.replace(/\n$/, "");
runProgram(read);
});
process.on("SIGINT", function () {
read = read.replace(/\n$/, "");
runProgram(read);
process.exit(0);
});
}