Skip to content

Bug at Frequency Counter problem #10

@HedrisTemmyTop

Description

@HedrisTemmyTop

Hello, Nice work there! thank you for solutions provided. However i want to call your attention to a bug at problem solving pattern/frequency counter/areThereDuplicates

the solution provided will fail in a scenario where it's comparing numbers and strings that are the same

for example, if the input is areThereDuplicates(1, '1')

the solution provided is going to return true instead of false because string of 1 and number of 1 is not a duplicate, and this is because of the way javascript object works, the javascipt keys would be string when you pased number

FURTHER EXPLANATION
At first iteration, the const lookup = {}; would be empty

it checks if lookup[1] exists which is no
it then adds it to the object but the key would be a string : {'1': 1}

at second iteration where args[i] = '1'

it checks for lookup['1'] which it is going to find in the object and returns true, that it has found duplicates.

but here's my solution after researching the web
we should use built in javascript Map instead, which accepts number as key


function areThereDuplicates(...everythingPassed) {


  if (!everythingPassed.length) return false;
  let frequencyCount = new Map();
  for (let i = 0; i < everythingPassed.length; i++) {
    if (frequencyCount.get(everythingPassed[i])) return true;

    frequencyCount.set(everythingPassed[i], 1);
  }

  return false;
}

console.log(areThereDuplicates(1, "1")); // false

THANKS

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions