rarestChar

JavaScript performance comparison

Test case created

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Fixed
var VidderInterview = {
  rarestChar: function(str) {
    var strLen = str.length,
        currFreq = 0,
        smallChar = str.charAt(0),
        freqs = {},
        uniqChars = [smallChar],
        i = 1,
        currChar = null;

    freqs[smallChar] = 1;

    for (; i < strLen; ++i) {
      currChar = str.charAt(i);
      if (freqs[currChar] !== undefined) {
        currFreq = ++freqs[currChar];
      } else {
        currFreq = 1;
        freqs[currChar] = currFreq;
        uniqChars.push(currChar);
      }
    }

    strLen = uniqChars.length;
    for (i = 1; i < strLen; ++i) {
      currChar = uniqChars[i];
      if (freqs[currChar] < freqs[smallChar]) smallChar = currChar;
    }

    return smallChar;
  }
};

VidderInterview.rarestChar('aaacabbcbbbc');
pending…
Broken
var VidderInterview = {
    rarestChar: function(str) {
        var smallChar = str.charAt(0),
            uniqChars = [smallChar],
            currChar = null,
            currFreq = 0,
            freqs = {},
            uniqIndex = 0;
        freqs[smallChar] = 1
       
        for (var i = 1, len = str.length; i < len; ++i) {
            currChar = str.charAt(i);
            if (freqs[currChar] !== undefined)
                currFreq = ++freqs[currChar];
            else {
                currFreq = 1;
                uniqChars.push(currChar);
                freqs[currChar] = currFreq;
                ++uniqIndex;
            }
            if (currFreq < freqs[smallChar]) {
                smallChar = currChar;
                smallIndex = uniqIndex;
            }
        }

        while (smallIndex > 0) {
            currChar = uniqChars[smallIndex];
            if (freqs[currChar] <= freqs[smallChar])
                smallChar = currChar
            --smallIndex;
        }
       
        return smallChar;
    }
};

VidderInterview.rarestChar('aaacabbcbbbc');
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment