Quick actions

cmd+k|ctrl+k

Navigation

Languages

repeat

Snippet info

Language

JavaScript

Visibility

public

Author

rovanova

Created

2020-03-30T19:03:44Z

Updated

2020-03-30T19:03:56Z

//Google Question
//Given an array = [2,5,1,2,3,5,1,2,4]:
//It should return 2

//Given an array = [2,1,1,2,3,5,1,2,4]:
//It should return 1

//Given an array = [2,3,4,5]:
//It should return undefined


function firstRecurringCharacter(input){
  rep = {};
  for(let i=0;i<input.length;i++){
    // console.log(rep);
    if(rep[input[i]]){
      return input[i];
    }
    else if(!rep[input[i]]){
      rep[input[i]]=true;
    }
  }
  return undefined;
} 

array = [2,3,4,5,2]
// firstRecurringCharacter(array)
console.log(firstRecurringCharacter(array));


//Bonus... What if we had this:
// [2,5,5,2,3,5,1,2,4]
// return 5 because the pairs are before 2,2
INFO