Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Frequency Counter Question #180

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions content/questions/frequency-counter/index.md
Original file line number Diff line number Diff line change
@@ -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]));
```
<!-- explanation -->
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]));
```