Pinncik
Pinncik

Reputation: 321

How to get the most repeated value in array

I'm struggling on simple exercise for an About 2 hours. I've seen here on Stack Overflow similar questions to this but I don't understand how is it possible with all of that weird notation or why and for what do I have to use object for getting result.

My question is how to get value from array which is the most repeated and count how many times it is in the array.

I've tried following. This code does nothing. This is my try number 292827, but every time I want to give up I've got another idea how to solve it but it's worse and worse. I'm just looking for simpler syntax than spreading object into atom molecules and some other alien syntaxes.

var arr = ['h', 'a', 'h', 'p', 'h'];

function findDuplicate(arr) {
  arr.sort();
  let count = 0;
  let current;

  for (var i = 0; i < arr.length; i++) {
    if (arr[i] === arr[i + 1]) {
      current = arr[i];
      count++;
    } else {
      current = arr[i]
    }

    return current + count
  }
}

console.log(findDuplicate(arr))

Upvotes: 0

Views: 527

Answers (2)

Phindette
Phindette

Reputation: 33

You can use 2 for :

One to check each elements of the list And the second isnide the first one to check how much times it is repeated.

At the end of the first for you check if the number you checked is the biggest, if it is then change the values for this one.

var arr = ['h', 'a', 'h', 'p', 'h'];

function findDuplicate(arr) {
  arr.sort();

  let currentMostRepeatedNumber = arr[0];
  let currentNumberOfTimeRepeated = 1;

  for (var i = 0; i < arr.length; i++){

      //We initiate the current number we'll check in this iteration
      let currentRepeat = arr[i];
      let amountOfRepetition = 1;

      //We start a for to check the amount of times the number is repeated
      for(var y= 0; y < arr.length;y++){
        
        if(currentRepeat === arr[y]){
            amountOfRepetition++;
        }  
      }
 
      if(amountOfRepetition > currentNumberOfTimeRepeated ){
            currentNumberOfTimeRepeated = amountOfRepetition;
            currentMostRepeatedNumber = currentRepeat ;
       }
  }
    return "The most repeated number is " +currentMostRepeatedNumber + " , and he is repetead " + currentNumberOfTimeRepeated +" amount of times"
}

console.log(findDuplicate(arr))

Upvotes: 0

Barmar
Barmar

Reputation: 780869

Start by initializing current to the first element of the array. Then loop over the remaining elements.

When the current element is the same as current, increment the counter. When it's different, reset count and current.

In order to get the highest count, you need another variable to hold that. Whenever you reset count, check if it's higher than the highest and replace that.

And move the return statement out of the loop.

var arr = ['h', 'a', 'h', 'p', 'h'];

function findDuplicate(arr) {
  arr.sort();
  let count = 1;
  let current = arr[1];
  let maxcount = 1;
  let maxelement = current;

  for (var i = 1; i < arr.length; i++) {
    if (arr[i] === current) {
      count++;
    } else {
      if (count > maxcount) {
        maxcount = count;
        maxelement = current;
      }
      current = arr[i];
      count = 1;
    }
  }
  // in case the last element has the highest count
  if (count > maxcount) {
    maxcount = count;
    maxelement = current;
  }

  return maxelement + maxcount;
}

console.log(findDuplicate(arr))

Upvotes: 2

Related Questions