educative.io

A item with same Key already exists

when i converted this code c# ,it gave following exception " A item with same Key already exists. I am wondering why the same exception is not coming for Java code being suggested here.

if the character is already present in map , we are setting windowStart pointer, but we are not removing the character from the map or clearing the map completely. Without removing we are adding the character

charIndexMap.put(rightChar, windowEnd); // insert the ‘rightChar’ into the map

This should ideally give exception. Could you please explain the logic and also why its not throwing exception

if (charIndexMap.containsKey(rightChar)) {

    // this is tricky; in the current window, we will not have any 'rightChar' after its previous index

    // and if 'windowStart' is already ahead of the last index of 'rightChar', we'll keep 'windowStart'

    windowStart = Math.max(windowStart, charIndexMap.get(rightChar) + 1);

  }

  charIndexMap.put(rightChar, windowEnd); // insert the 'rightChar' into the map

Hi @santhosh_krishnamurt. Can you please share the lesson link?