From 7e44f6014a4b8dbca5e7ae0a75a762fda0fe0355 Mon Sep 17 00:00:00 2001 From: asuju Date: Sat, 3 Oct 2020 12:35:55 +0530 Subject: [PATCH] Frequency Counter Question --- content/questions/frequency-counter/index.md | 48 ++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 content/questions/frequency-counter/index.md diff --git a/content/questions/frequency-counter/index.md b/content/questions/frequency-counter/index.md new file mode 100644 index 00000000..d4cd70be --- /dev/null +++ b/content/questions/frequency-counter/index.md @@ -0,0 +1,48 @@ +--- +title: Frequency Counter +tags: + - Array + - Key +order: 75 +date: Sat Oct 03 2020 11:37:42 GMT+0530 (India Standard Time) +answers: + - 'True // correct' + - 'False' +--- + +Consider the following code block. What is the correct Output? +```javascript +function frequencyCounter(arr1, arr2){ +if(arr1.length !== arr2.length){ + return false; +} +for(let i = 0; i < arr1.length; i++){ + let correctIndex = arr2.indexOf(arr1[i] ** 2) + if(correctIndex === -1) { + return false; + } + arr2.splice(correctIndex,1) +} + return true; +} +console.log(frequencyCounter([1,2,3,2], [9,1,4,4])); +``` + +The function should return true if every value in the array has it's corresponding value squared in the second array.Also frequency of values must be the same.Below Block will give output as false. + +```javascript +function frequencyCounter(arr1, arr2){ +if(arr1.length !== arr2.length){ + return false; +} +for(let i = 0; i < arr1.length; i++){ + let correctIndex = arr2.indexOf(arr1[i] ** 2) + if(correctIndex === -1) { + return false; + } + arr2.splice(correctIndex,1) +} + return true; +} +console.log(frequencyCounter([1,2,3,2], [9,1])); +``` \ No newline at end of file