-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrays_problems.js
More file actions
47 lines (44 loc) · 1009 Bytes
/
Copy patharrays_problems.js
File metadata and controls
47 lines (44 loc) · 1009 Bytes
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
//cyclic rotation
function solution(A,K){
let AL = A.length;
if(AL ===0 || K ===o){
return A;
}
let K = K % AL;
for (let i =0; i < AL; i++){
let newIndex = (i+k) %AL;
rotatedArray[newIndex] = A[i];
}
return rotatedArray;
}
//odd occurences in array
function solution(A){
let AL = A.length;
for(let i = 0; i < AL; i++){
result ^= A[i];
}
return result;
}
// odd occurences in array solution2
function solution2(A){
let AL = A.length;
//creat a map to store the count of each element
const countMap = {};
// iterate this array and count the occurences of each element
for (let i= 0; i< AL; i++){
countNum = A[i];
if (countMap[countNum] = undefined){
countMap[countNum] = 1;
}else{
countMap[countNum]++;
}
}
// iteratethe whole map and find the elemnt with odd count
for (const num in countMap){
if (countMap[key] % 2!==0){
return parseInt(key);
}
}
//if no odd occurences number, return -1
return -1;
}