-2

So let's say I have a two arrays which consists of numbers.

var arr1 = [1,2,3,4,5];
var arr2 = [1,1,1,4,4,5,5,2];

Is it possible to compare this two arrays in order to get most repeated number in two of them (for this example this number would be "1")? There could be any numbers of any value in arrays.

Dazvolt
  • 697
  • 2
  • 10
  • 22

1 Answers1

0

Try this

        function maxAppearedNos()
        {
            var arr1 = [1,2,3,4,5,2,2,2,2,2,2,2,2];
            var arr2 = [1,1,1,4,4,5,5,2];
            var concatenatedArray = arr1.concat(arr2); 
            var macCountNos = getMaxAppearedNos(concatenatedArray);
        }

        function getMaxAppearedNos(array)
        {
            if(array.length == 0)
                return null;
            var modeMap = {};
            var maxEl = array[0], maxCount = 1;
            for(var i = 0; i < array.length; i++)
            {
                var el = array[i];
                if(modeMap[el] == null)
                    modeMap[el] = 1;
                else
                    modeMap[el]++;  
                if(modeMap[el] > maxCount)
                {
                    maxEl = el;
                    maxCount = modeMap[el];
                }
            }
            return maxEl;
        }
        maxAppearedNos();
Newinjava
  • 972
  • 1
  • 12
  • 19