1

I have an array: Array[[List[Int]]](81) ( this is a val )

I want to break it down say row-wise and if any of the elements in that particular row has only one element in the list, say b.size = 1, then I want to remove that element in 'b', from every other list in that particular row of the Array.

I'm not sure, how to go about considering just the first 9 elements, then the next 9 elements from the Array, given the constraint that I don't use mutable variables. ( so just vals and not vars ).

So far: I have a function that accepts the list and the element to be removed from it. But, how do I go about iterating through every row in the above array or every column in the array, and go back to the starting of the row, if I find a list with just one element, to update the other elements in the list, is my question.

2
  • 1
    do you mean you have a Array[Array[Any]] ? Commented Nov 23, 2012 at 4:40
  • Please have a go at rewriting your question. And provide the function you currently have also. Commented Nov 23, 2012 at 4:44

1 Answer 1

1

Assuming you mean you have an Array[Array[List[Int]]], this sounds like it'll do what you're after:

scala> def filterRow(arr:Array[List[Int]]):Array[List[Int]] = {
     |   val found:Option[Int] = arr.collectFirst{case List(x) => x};
     |   found.map(f =>
     |     arr.map(l =>
     |       if (l.size == 1) l else l.filterNot(_ == f)
     |     )
     |   ).getOrElse(arr)
     | }
filterRow: (arr: Array[List[Int]])Array[List[Int]]
scala> val a = Array(Array(List(1),List(1,2),List(1,2,3)),Array(List(2),List(1,2),List(1,2,3)))
a: Array[Array[List[Int]]] = Array(Array(List(1), List(1, 2), List(1, 2, 3)), Array(List(2), List(1, 2), List(1, 2, 3)))
scala> a.map(filterRow)
res0: Array[Array[List[Int]]] = Array(Array(List(1), List(2), List(2, 3)), Array(List(2), List(1), List(1, 3)))

If not, hopefully it gives you enough of a lead to work it out. Otherwise, you might need to clarify your question a bit more.

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

Comments

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.