Skip to content

Instantly share code, notes, and snippets.

@michalmikolajczyk
Created January 8, 2015 22:11

Revisions

  1. michalmikolajczyk created this gist Jan 8, 2015.
    54 changes: 54 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@


    var A = [];
    A[0] = 3;
    A[1] = 5;
    A[2] = 6;
    A[3] = 3;
    A[4] = 3;
    A[5] = 5;

    function identicalIndices(someArray) {
    var count = 0;
    someArray.forEach(function (item, index) {
    var restOfArray = someArray.slice(index + 1, someArray.length);
    restOfArray.forEach(function (pairCandidate) {
    if (pairCandidate === item) {
    count++;
    }
    });

    });

    return count;
    }

    var b = identicalIndices(A);
    console.log(b);

    }

    // Another way to do this is to create a helper object
    // which stores the indices like this
    // var helper = {};
    // var count = 0
    // A.forEach(function (item, index) {
    // if (helper[item]) {
    // helper[item].push(index);
    // } else {
    // helper[item] = [index];
    // }
    // });

    // for (var key in helper) {
    // if (helper.hasOwnProperty((key…


    // if (helperObject[item]) {
    // helperObject[item]++;
    // if (helperObject[item] % 2 === 0) {
    // count++;
    // }
    // } else {
    // helperObject[item] = 1;
    // }