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.