-3

For Example A = [10010,10020,99948] and each element of A List possible values are atmost two values or one element or null.

you can take below is B list,

  • 10010 = []. --> expected output is 10010
  • 10020 =[10020,10020] -> expected output is null
  • 99948 = [99948,99948] -> expected output is null

so final output should be 10010.

MyCode is working fine but i need better solution below the O(n).

var list = new ArrayList()
for ( val in A){
   var B = val.Valus.toList()
   if ( not B.contain(val)){
       list.add(val)
    }
}

Here output is : 10010

if A has 2000+ values it performance down , can be do better?

Buddy Bob
  • 5,829
  • 1
  • 13
  • 44
Py-Coder
  • 2,024
  • 1
  • 22
  • 28

2 Answers2

3

This is a very confusing question. The title describes it as finding unique values in two arraylists. And then you're asking for something with a runtime below O(n), which would be linear runtime. There's no way you'd be able to create such an algorithm, because at the very least you'd have to read every item of both lists once. If you're looking for something better than O(n^2), that's definitely doable. Just add the contents of both arrays to a new array. Then quicksort the array (n log n). Then traverse the array looking for non-repeated values, and you've found the uniques, and you've got O((n log n) + 3n), which will scale much better than O(n^2)

The algorithm you posted doesn't solve the problem you posed at all. You've got A as an array (not an arraylist) and then you've got this weird compilation error:

var B = val.Valus.toList()

where val is an int, and so val.Valus will not work.

The algorithm you propose, even if it works, wouldn't be O(n), it'd be O(n^2). I submit that there is no algorithm which would solve this problem that would work in better than linear time.

Domenick Doran
  • 261
  • 1
  • 7
  • +1 to this answer. Finding all the unique values of 2 ArrayLists can't be done in linear time. To the OP of this question, maybe post some business context of what you are trying to do in one of the Guidewire applications. What do the values in each ArrayList represent in a Guidewire application? – SteveDrippsCentricConsulting Jul 21 '23 at 14:09
0

you can utilize the characteristic: Utilization characteristics values are atmost two values or one element or null.

  1. new a output list

  2. loop on your input list

    a) if the elem in output list, del the item in list

    b) if the elem not in output list, add it to output list

finally, the output list will contails all unique elems.

Xiaomin Wu
  • 400
  • 1
  • 5