Last active
May 21, 2025 06:56
-
-
Save salmanx/d3420ceafca026ed9c2199b170daeaae to your computer and use it in GitHub Desktop.
air work coding test
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
// Problem 1: Move Zeroes | |
function moveZeroToEnd(arr) { | |
const count = {}; | |
const result = []; | |
arr.forEach((element) => { | |
count[element] = (count[element] || 0) + 1; | |
}); | |
if (count[0] > 0) { | |
for (let i = 0; i < count[0]; i++) { | |
let index = arr.findIndex((item) => item === 0); | |
delete arr[index]; | |
result.push(0); | |
} | |
} | |
return arr.flat().concat(result); | |
} | |
console.log(moveZeroToEnd([0, 1, 0, 3, 12])); // [ 1, 3, 12, 0, 0 ] | |
console.log(moveZeroToEnd([0])); // [ 0 ] | |
// Problem 3: Find First Unique Character in a String | |
function findNonRepetaingChar(str) { | |
let result; | |
const count = {}; | |
Array.from(str).forEach((element) => { | |
count[element.toLowerCase()] = (count[element.toLowerCase()] || 0) + 1; | |
}); | |
const repeating = Object.entries(count); | |
for (let index = 0; index < repeating.length; index++) { | |
let item = repeating[index]; | |
if (item[1] === 1 && item[0] !== "") { | |
result = item[0]; | |
break; | |
} else { | |
result = -1; | |
} | |
} | |
return str.split("").findIndex((i) => i == result); | |
} | |
console.log(findNonRepetaingChar("Talent knows no boundaries")); // 2 | |
console.log(findNonRepetaingChar("aabb")); // -1 | |
// Problem 2: Intersection of Two Arrays | |
function intersection(nums1, nums2) { | |
let set1 = new Set(nums1); | |
let resultSet = new Set(); | |
for (let num of nums2) { | |
if (set1.has(num)) { | |
resultSet.add(num); | |
} | |
} | |
return Array.from(resultSet); | |
} | |
console.log(intersection([1, 2, 2, 1], [2, 2])); // [2] | |
console.log(intersection([4, 9, 5], [9, 4, 9, 8, 4])); // [9, 4] | |
// union: return [...new Set([...arr1, ...arr2])]; | |
// first non-repeating character and then collects all non-repeating characters into an array: | |
const str = "swiss"; | |
// Step 1: Build frequency map | |
const freq = {}; | |
for (const char of str) { | |
freq[char] = (freq[char] || 0) + 1; | |
} | |
// Step 2: Find first non-repeating character | |
let firstNonRepeating = null; | |
for (const char of str) { | |
if (freq[char] === 1) { | |
firstNonRepeating = char; | |
break; | |
} | |
} | |
// Step 3: Collect all non-repeating characters | |
const allNonRepeating = []; | |
for (const char of str) { | |
if (freq[char] === 1) { | |
allNonRepeating.push(char); | |
} | |
} | |
console.log("First non-repeating character:", firstNonRepeating); // "w" | |
console.log("All non-repeating characters:", allNonRepeating); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment