This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| console.log(firstRepeat("legolas")) | |
| console.log(firstRepeat("Gandalf")) | |
| console.log(firstRepeat("Balrog")) | |
| console.log(firstRepeat("Isildur")) | |
| function firstRepeat(str) { | |
| const seen = {}; | |
| for (let char of str) { | |
| if(seen[char]) return char | |
| seen[char] = 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // isPositiveDominant([1, 1, 1, 1, -3, -4])// false | |
| // // There is only 1 unique positive value (1). | |
| // // There are 2 unique negative values (-3, -4). | |
| // isPositiveDominant([5, 99, 832, -3, -4])// true | |
| // isPositiveDominant([5, 0])// true | |
| isPositiveDominant([0, -4, -1])// false | |
| function isPositiveDominant(arr) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function sortByLength(arr = []) { | |
| for (let j = 0; j < arr.length; j++) { | |
| for (let i = 0; i < arr.length - j; i++) { | |
| const [x, y] = [arr[i], arr[i + 1]] | |
| if (arr[i + 1] && arr[i].length > arr[i + 1].length) swap(arr, i, i + 1) | |
| } | |
| } | |
| console.log(arr) | |
| return arr |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function nQueenPlacement(board) { | |
| let rowPositions = []; | |
| let colPositions = []; | |
| for (let i = 0; i < board.length; i++) { | |
| for (let j = 0; j < board[i].length; j++) { | |
| if (board[i][j] === 1) { | |
| if (rowPositions.indexOf(i) === -1 && colPositions.indexOf(j) === -1 && pairModulusDiffers(rowPositions, colPositions, i, j)) { | |
| rowPositions.push(i); | |
| colPositions.push(j); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function decipherThis(string) { | |
| const words = string.split(' '); | |
| return words.map(word => decode(word.split(''))).join(' '); | |
| } | |
| function decode(wordArr) { | |
| let hex = '' | |
| let secChar | |
| for (let [idx, char] of wordArr.entries()) { | |
| if (Number(char) || char == 0) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function fold(arr) { | |
| let start = 0; | |
| let end = arr.length - 1; | |
| const res = [] | |
| while (start < end) { | |
| res.push(arr[start] + arr[end]); | |
| start++; | |
| end--; | |
| } | |
| if (arr.length % 2 === 1) res.push(arr[start]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function onesInfection(matrix) { | |
| const rowSet = new Set() | |
| const colSet = new Set() | |
| for (let i = 0; i < matrix.length; i++) { | |
| for (let j = 0; j < matrix[i].length; j++) { | |
| if (matrix[i][j] == 1) { | |
| rowSet.add(i) | |
| colSet.add(j) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const country_codes_array = ["AF","AL","DZ","AS","AD","AO","AI","AQ","AG","AR","AM","AW","AU","AT","AZ","BS","BH","BD","BB","BY","BE","BZ","BJ","BM","BT","BO","BQ","BA","BW","BV","BR","IO","BN","BG","BF","BI","KH","CM","CA","CV","KY","CF","TD","CL","CN","CX","CC","CO","KM","CG","CD","CK","CR","HR","CU","CW","CY","CZ","CI","DK","DJ","DM","DO","EC","EG","SV","GQ","ER","EE","ET","FK","FO","FJ","FI","FR","GF","PF","TF","GA","GM","GE","DE","GH","GI","GR","GL","GD","GP","GU","GT","GG","GN","GW","GY","HT","HM","VA","HN","HK","HU","IS","IN","ID","IR","IQ","IE","IM","IL","IT","JM","JP","JE","JO","KZ","KE","KI","KP","KR","KW","KG","LA","LV","LB","LS","LR","LY","LI","LT","LU","MO","MK","MG","MW","MY","MV","ML","MT","MH","MQ","MR","MU","YT","MX","FM","MD","MC","MN","ME","MS","MA","MZ","MM","NA","NR","NP","NL","NC","NZ","NI","NE","NG","NU","NF","MP","NO","OM","PK","PW","PS","PA","PG","PY","PE","PH","PN","PL","PT","PR","QA","RO","RU","RW","RE","BL","SH","KN","LC","MF","PM","VC","WS","SM","ST","SA","SN","RS","SC","SL","SG", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function maxSubarray(arr, k) { | |
| if (arr.length < k) return 0 | |
| let currSubSum = arr.slice(0, k).reduce((a, b) => a + b); | |
| let sum = currSubSum; | |
| for (let i = k; i < arr.length; i++) { | |
| currSubSum = currSubSum + arr[i] - arr[i - k] | |
| sum = Math.max(sum, currSubSum) | |
| } | |
| return sum | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function getMaxProfit(array) { | |
| let profit = 0; | |
| for (let i = 0; i < array.length; i++) { | |
| let max = array[i] | |
| for (let j = i + 1; j < array.length; j++) { | |
| if (max < array[j]) max = array[j] | |
| } | |
| profit += max - array[i] | |
| } | |
| return profit |