Date: - 31 January 2024
(1) write a program to take an array input and return the two arrays one of unique numbers and one for duplicate numbers: -
const findDuplicateNumber=(a)=>{
const duplicateObject = {};
const uniqueArray = []
const duplicateArray = []
for(let numbers of a){
duplicateObject[numbers] = (duplicateObject[numbers] || 0) + 1
}
for(let keys in duplicateObject){
if(duplicateObject[keys] > 1){
duplicateArray.push(keys)
}else{
uniqueArray.push(keys)
}
}
return {duplicateArray, uniqueArray}
}
testingArray=[2,4,5,55,16,22,55,16,5]
const duplicateNumber = findDuplicateNumber(testingArray)
console.log(duplicateNumber.duplicateArray)
console.log(duplicateNumber.uniqueArray)
(2) Write a program to find if a string is an anagram or not?
const isAnagram=(name1, name2)=>{
if(name1.length != name2.length){
console.log(name1,"and",name2, "can not be Anagram")
}else{
let counter = {}
for(let numbers of name1){
counter[numbers] = (counter[numbers] || 0) + 1
}
for(let numbers2 of name2){
if(!counter[numbers2]){
console.log("this can not be anagram===")
return
}
counter[numbers2] -= 1
}
console.log("This both strings are anagram")
}
}
const name1 = "jokar";
const name2 = "okelr";
isAnagram(name1,name2);