Quick actions

cmd+k|ctrl+k

Navigation

Languages

Google Question

Snippet info

Language

Dart

Visibility

public

Author

obihenry578

Created

2025-02-09T09:05:18.650373Z

Updated

2025-02-09T11:23:51.017682Z

void main() {
    // return reoccured number
 //Google Question
List array = [2,5,5,2,3,5,1,2,4];
//It should return 2

List array1 = [2,1,1,2,3,5,1,2,4];
//It should return 1

List array2 = [2,3,4,5];
//It should return undefined

// detect 5 and 5 firast
//[2,5,5,2,3,5,1,2,4];
 firstRecurringCharacter0(input) {
     List temp = [];
  for (var i = 0; i < input.length; i++) {
       int count = 0;
    for (var j = i + 1; j < input.length; j++) {
      if(input[i] == input[j]) {
         count++ ;
          if(count ==2){
               return input[j];
          }
      }
    }
  }
  return 'undefined';
}


 String firstRecurringCharacter(input) {
    List tempList = [];
    String value = 'undefined';
    
    for(var i = 0; i < input.length; i++){
        
        if(tempList.contains(input[i])){
            value = input[i].toString();
            break;
        }else{
            value = 'undefined';
        }
        tempList.add(input[i]);
    }
    
    return value;
}

 String firstRecurringCharacter1(input) {
    Map tempList = {};
    String value = 'undefined';
    
    for(var i = 0; i < input.length; i++){
        print(tempList);
        if(tempList.containsKey(input[i])){
            value = input[i].toString();
            break;
        }else{
            value = 'undefined';
        }
        tempList[input[i]] = i;
    }
    
    return value;
}

print(firstRecurringCharacter0(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