0

I'm trying to convert strings to arrays then compare two arrays. If the same value needs to remove from both array. Then finally merge two arrays and find array length. Below is my code

  String first_name = "siva";
  String second_name = "lovee";
  List<String> firstnameArray=new List();
  List<String> secondnameArray=new List();
  firstnameArray = first_name.split('');      
  secondnameArray = second_name.split('');
   var totalcount=0;
  for (int i = 0; i < first_name.length; i++) {
    for (int j = 0; j < second_name.length; j++) {
      if (firstnameArray[i] == secondnameArray[j]) {
        print(firstnameArray[i] + "" + " == " + secondnameArray[j]);
        firstnameArray.removeAt(i); 
        secondnameArray.removeAt(i); 
        break;

      }
    }
  }
  var finalList = new List.from(firstnameArray)..addAll(secondnameArray);
  print(finalList);
  print(finalList.length);

But always getting this error Unsupported operation: Cannot remove from a fixed-length list can you help me how to fix this issue. Thanks.

2 Answers 2

2

Seems like what you are trying to do is to find the length of unique characters in given two strings. Well, the Set type is perfect for this use-case. Here's an example of what you can do:

void main() {
  String first = 'abigail';
  String second = 'allie';

  var unique = '$first$second'.split('').toSet();
  print(unique);
}

This would give you an output of:

{a, b, i, g, l, e}

On which you may perform functions like .toList(), or .where() or .length.

Sign up to request clarification or add additional context in comments.

1 Comment

Input: String first = 'sivaaa'; String second = 'lovea'; Output: siaaloe Above is my expected output not unique characters. Do you have any idea about that?
1

You can ensure that firstnameArray, secondnameArray is not a fixed-length list by initializing it as below:

var firstnameArray = new List<String>.from(first_name.split(''));
var secondnameArray= new List<String>.from(second_name.split(''));

Thereby declaring firstnameArray, secondnameArray to be a mutable copy of input.

1 Comment

Input: String first = 'sivaaa'; String second = 'lovea'; Output: siaaloe Above is my expected output not unique characters. Do you have any idea about that? i changed that line but getting range error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.