• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 909
  • Last Modified:

Array comparison algorithm of different sized arrays

Hello all,

I seem to have a brain freeze going and I sure could use some help before I do something drastic (you know I never really liked that guy in the next cubicle)!

Assume I have 2 arrays.  Array 1 is a list of possible string values:

final String colors[] = {"black", "brown", "red", "orange", "yellow",
                            "green", "blue", "violet","grey", "white"};

Array 2 is a list of 3 colors chosen by a user:

final String userColors[] = {"red", "yellow", "white"};

How can I compare the two arrays to make sure that userColors[] only has values that can be found in colors[]?

Thanks,
Brian
0
bnblazer
Asked:
bnblazer
1 Solution
 
imladrisCommented:
In pseudo code it would be something like:

set usercolorsvalid to true
for all user colors
     set incolor to false
     for all colors
          if current user color equals current color
               set incolor to true
               break
     if not incolor set usercolorsvalid to false

at the end usercolorsvalid will indicate whether all user colors are in the colors array or not. Basically, you go through them one by one, and check if there is an entry for them in the colors array
0
 
owenli27Commented:

if ((Arrays.asList(colors)).containsAll(Arrays.asList(userColors) ))
{
....
}
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now