Win a copy of Kotlin in Action this week in the Kotlin forum!
    Bookmark Topic Watch Topic
  • New Topic

How can I get the unmatched values from two arrays  RSS feed

 
babu javagwcc
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi ,

How can I get the unmatched values from two arrays, like
Scenario1:

Array1[1,2,3,4]
Array1[1,2,3,4, 5]
I should get the result ‘5’

Scenario2:

Array1[1,2,3,4,5,6]
Array1[1,2,3,4, 5]
I should get the result ‘6’

Scenario3:
Array1[]
Array1[1,2,3,4, 5]
I should get the result ‘1,2,3,4,5’

Mean time performance wise it should not take much time. May be 'Collections' will help

Very thankful for replies in advance.

-Babu
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
babu javagwcc wrote:Hi ,

How can I get the unmatched values from two arrays, like
Scenario1:

Array1[1,2,3,4]
Array1[1,2,3,4, 5]
I should get the result ‘5’

Scenario2:

Array1[1,2,3,4,5,6]
Array1[1,2,3,4, 5]
I should get the result ‘6’

Scenario3:
Array1[]
Array1[1,2,3,4, 5]
I should get the result ‘1,2,3,4,5’

Mean time performance wise it should not take much time. May be 'Collections' will help

Very thankful for replies in advance.

-Babu


I think there's no such method defined in Collections or in Arrays, that can be used here..
May be you have to do in manually.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Report post to moderator
You would need to define exactly what you mean abotu retaining things. Are you doing it by hand or can you use the bulk operations of the Set interface? You will find that in the Java tutorials. What about [1, 2, 3, 4, 5, 6, 6, 6, 6, 6, 7]/[1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5]? What will happen if you compare those two arrays? Are you dealing with sets or sequences (lists)? You will find more about sets and lists in the link I posted.
 
jatan bhavsar
Ranch Hand
Posts: 299
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi Babu,

you can try this logic and utilize. you can try to optimize this as well.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
babu javagwcc wrote:Mean time performance wise it should not take much time. May be 'Collections' will help...

If performance is important then yes, using another type of dataset would probably be a good idea; but as Campbell said, you must be absolutely clear about your matching (or in your case non-matching) process first.

This recent thread is on a similar subject, and may contain stuff you find useful.

Winston
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
jatan bhavsar wrote:Hi Babu,

you can try this logic and utilize. you can try to optimize this as well.

This is not a very good way to code if you are using Java.. Seems like we're back to C.
If you want to do it manually, may be you should convert the array into ArrayList and use pre-defined method to help you achieve what you are doing with "==".

Still, the thing which Campbell and Winston have quoted above has to be considered..
 
jatan bhavsar
Ranch Hand
Posts: 299
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi ,

It seems Babu is looking for solution with array that is why i provided this solutions.

R. Jain can you please guide me other better approach using arraylist predefine method.. specially predefine method for this ?





Regards
Jatan
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
jatan bhavsar wrote:It seems Babu is looking for solution with array that is why i provided this solutions.

Jatan, we try not to provide ready-made solutions here at JavaRanch; it's much better to let people discover them for themselves.
For one thing, you can look awfully foolish if the solution is poor (or worse still, wrong).

I suggest you read the the HowToAnswerQuestionsOnJavaRanch (←click) page as a guide.

Winston
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
jatan bhavsar wrote:Hi ,
It seems Babu is looking for solution with array that is why i provided this solutions.
Regards
Jatan


You can always convert an Array into a List and operate on them.. ( As operating on a List is always easier than that on an Array)

jatan bhavsar wrote:Hi ,
R. Jain can you please guide me other better approach using arraylist predefine method.. specially predefine method for this ?


I think there is no pre-defined method in ArrayList to perform a Disjoint operation on two ArrayLists..
But while you do it manually, ArrayList is a better option than Array.. (But of course, iterating over each element of an ArrayList and then comparing is not such a good idea)
And at the moment, I am unable to think of a better way to achieve this task.. Hope someone else will come up with a better solution soon..
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
R. Jain wrote:If you want to do it manually...

R. Jain,

Style point: I suggest that you don't always include entire posts in your replies. A few lines will usually do; otherwise it makes threads excessively long for those of us with CTS to scroll through.

Thanks.

Winston
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
Check this post.. It has got what you want..
 
jatan bhavsar
Ranch Hand
Posts: 299
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi R Jain,

Can you please explain how can i achieve finding uncommon elements by using arraylist's method as i couldnt understand how to achieve it with the reference link and removeall method.

R. Jain you should convert the array into ArrayList and use pre-defined method
this was mention in your earlier post
I will be really thankful to you if you can provide me example.


you can look awfully foolish if the solution is poor


Atleast i tried to guide with the logic which was my main intense .

Regards
Jatan
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Report post to moderator
I sometimes only delete good solutions because other solutions can create interesting discussion about their shortcomings. I would like to see babu javagwcc’s response to my earlier post first, however.
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
jatan bhavsar wrote:Hi R Jain,
Can you please explain how can i achieve finding uncommon elements by using arraylist's method as i couldnt understand how to achieve it with the reference link and removeall method.

If you read that post clearly, you can do it.. I suggest you try it on your own.. Create two ArrayList with different elements.. And call removeAll method on list1 passing the second list(list2) as argument..
This methods updates the list on which it is called (list1).. So you can print the list1 again to see the changes..

jatan bhavsar wrote:
R. Jain you should convert the array into ArrayList and use pre-defined method
this was mention in your earlier post
I will be really thankful to you if you can provide me example.

See Arrays documentation for useful methods..

 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!