Practice Resource - JS

Ques 1. Write a function called getUniqueCharacters that takes a string as input and returns an array of all the unique characters (case-sensitive) in the string. The output array should only contain unique characters, and the order of characters in the output array should match the order of their first occurrence in the input string.

Function signature:

function getUniqueCharacters(str) {

}

Example usage:


console.log(getUniqueCharacters("hello")); // Output: ["h", "e", "l", "o"]
console.log(getUniqueCharacters("world")); // Output: ["w", "o", "r", "l", "d"]
console.log(getUniqueCharacters("javascript")); // Output: ["j", "a", "v", "s", "c", "r", "i", "p", "t"]
console.log(getUniqueCharacters("aaaaaa")); // Output: ["a"]
console.log(getUniqueCharacters("12345")); // Output: ["1", "2", "3", "4", "5"]

Constraints:

* The input string `str` will only contain printable ASCII characters.
* The input string `str` may be empty.
* The output array should only contain unique characters, even if they appear more than once in the input string.
* The input string `str` can have a maximum length of 10^5 characters.

Ques 2. Write a function called findPairs that takes an array of integers and a target sum as input, and returns an array of arrays, each containing a pair of integers that sum up to the target sum. Each pair of integers should be sorted in ascending order, and there should be no duplicate pairs in the output array. If no pairs are found that sum up to the target sum, the function should return an empty array.

Function signature:

function findPairs(arr, targetSum) {

}

Example usage:

console.log(findPairs([1, 5, 3, 7, 8, 2, 6, 4], 9));
// Output: [[1, 8], [2, 7], [3, 6], [4, 5]] (pairs that sum up to 9)

console.log(findPairs([1, 2, 3, 4, 5], 10));
// Output: [[4, 6], [5, 5]] (pairs that sum up to 10)

console.log(findPairs([3, 1, 5, 7, 2, 6], 10));
// Output: [[3, 7], [5, 5], [2, 8]] (pairs that sum up to 10)

console.log(findPairs([4, 4, 4, 4, 4], 8));
// Output: [[4, 4]] (pairs that sum up to 8)

console.log(findPairs([1, 2, 3, 4, 5], 15));
// Output: [] (no pairs that sum up to 15)

Constraints:

  • The input array arr will contain only integers.
  • The input array arr may have duplicate elements.
  • The input array arr may have at least two elements and can have a maximum length of 10^4.
  • The target sum targetSum will be an integer.
  • The output array should not contain duplicate pairs.
  • The pairs in the output array should be sorted in ascending order of their first element.
2 Likes

For first question, use alphabets marking.
1- Make a boolean array of size 26.
2- Mark every element is boolean array as false;
3- Iterate thorugh thr given string, if alphabets array[i]==false, add to output array and mark the index as true.
4- return output array.

For question 2-

Simple 2 sum approach.
1- Load array to hashset for O(1) searching.
2- For every element, do diff= target-arr[i].
3- Check if hashset has diff in it. If yes, make pair and delete the diff from the hashset.
4- return pair array.

2 Likes

@kashifk9065
Q1 Ans- function getUniqueCharacters(str) {
const seen = {};
const result = [];

for (let i = 0; i < str.length; i++) {
const char = str[i];
if (!seen[char]) {
seen[char] = true;
result.push(char);
}
}

return result;
}

1 Like

@kashifk9065
Q2 Ans-function findPairs(arr, targetSum) {
const pairs = [];

for (let i = 0; i < arr.length - 1; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] + arr[j] === targetSum) {
const pair = [arr[i], arr[j]].sort((a, b) => a - b);
if (!pairs.some(p => p[0] === pair[0] && p[1] === pair[1])) {
pairs.push(pair);
}
}
}
}

return pairs;
}

1 Like

For second question
n= length of arr
m=length of pairs
Instead of doing it in O(m*n^2), we can do it in O(n) using Set in JS.

3 Likes