-2

How would I write a static method that takes two arrays of characters and returns true if every character contained in the first array is also contained in the second array, otherwise it return false. The characters could be in any order in the arrays, and it makes no difference if a character occurs more than once.

Here is what I have so far but I am confused as to how to complete the task

public static boolean compare(char[] arr1, char[] arr2)
{
   for(int i=0; i<arr1.length; i++)
   {



   }


}

Just for learning would it be possible to complete this task without using java built in methods

thanks

Raul Stephan
  • 25
  • 1
  • 4
  • http://stackoverflow.com/questions/245509/algorithm-to-tell-if-two-arrays-have-identical-members – Raghunandan May 12 '13 at 17:37
  • If the arrays contains only ASCII characters, create an array of boolean of size 256, for each letter in first array mark the proper index with true, do the same for the second array and compare what you got. – Bartosz Marcinkowski May 12 '13 at 17:40
  • Hi the link you have posted assumes no duplicates in my case I would like to factor in duplicates – Raul Stephan May 12 '13 at 17:43
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) [What have you tried?](http://www.whathaveyoutried.com/) I mean *besides* asking us. – Andrew Thompson May 12 '13 at 17:44
  • well I have tried to iterate through the second array comparing each indexed position for equality with the first array but am confused due to the potential random ordering of the arrays as well as well as ignoring duplicate values – Raul Stephan May 12 '13 at 17:47
  • This may be better suited to http://programmers.stackexchange.com/faq – Old Pro May 13 '13 at 01:41

6 Answers6

3

Divide the problem in two parts:

  1. write a method that checks if a character is contained in an array
  2. use this method to test that every character of the first array is contained in the second one. As soon as you find a character that is not contained in the second array, you can return false. At the end of the loop, return true.
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I like this answer most as it doesn't rely on library methods. If the question is related to a school exercise (which I highly suspect) I totally recommend using this approach as you'll learn very much from it. – nuala May 12 '13 at 17:42
  • 1
    @yoshi: I indeed took the question as a basic algorithmic exercise. Not as a question to find the most efficient algorithm or the most appropriate API method. – JB Nizet May 12 '13 at 17:45
2
  1. using Arrays.asList(array) convert them to collection.
  2. call fromArray1.containsAll(fromArray2) to get the result

As @JBNizet said, there's no Arrays.asList() for arrays of primitives.

But char can be converter to "collection" using one little trick - new String(charArray). String does not have .containsAll(charArray) method (it has... kind of - it's .equals(otherString)) so every character from array should be check separately by string.indexOf(char) != -1

dantuch
  • 9,123
  • 6
  • 45
  • 68
  • There is no Arrays.asList() method taking a char array as argument. – JB Nizet May 12 '13 at 17:44
  • @JBNizet, guess you're right. Converting `char[]` to `Character[]` just to be able to convert it to some collection later doesn't seem to be a good idea. – dantuch May 12 '13 at 17:47
1
public static boolean compare(char[] arr1, char[] arr2)
{
 String str = new String(arr2);
 //loop
 for(int i = 0; i < arr1.length; i++){
      if(str.indexOf(arr1[i]) == -1){
        return false;
      }
 }
 return true;
}

The idea is to make use of String.indexOf() method, that way you get a clean and clear code. The logic is to go through each character in the first array and see if it is in the second, if it is not return false.

if the loop ended successfully without returning then each char in A is in B.

Makky
  • 17,117
  • 17
  • 63
  • 86
Mr.Me
  • 9,192
  • 5
  • 39
  • 51
0

Create another array (or hashset) to use as the bins to count occurrence of characters.

First, run through the first input; set the bins corresponding to character in the first input to True.

Second, run through the second input; set the bins corresponding to character in the first input to False.

At the end, if the bins has no True value left, then every character in array 1 has occurred at least once in array 2.

Apiwat Chantawibul
  • 1,271
  • 1
  • 10
  • 20
0

Sort the second array at the top

Arrays.sort(arr2);

Add this line in your loop:

if(!Arrays.binarySearch(arr2, arr1[i])) return false;
Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
  • The characters could be in any order in the arrays. binarySearch only works if the array is sorted. – JB Nizet May 12 '13 at 17:40
  • @JBNizet You're right, I overlooked that. I fixed the answer. – Angelo Fuchs May 12 '13 at 17:42
  • So you've to sort before you can start comparing - not very elegant imho. – nuala May 12 '13 at 17:44
  • @yoshi I'd use the solution of dantuch because its the most elegant. But, considering the amount of different approaches for this problem, I think one more does not hurt. Also my approach alters the code of the OP the least (afaics) – Angelo Fuchs May 12 '13 at 17:47
  • It has a serious side-effect; the second array is modified by your algorithm. It shouldn't. – JB Nizet May 12 '13 at 17:48
0

no problem, just check every character separatly in the cycle:

public static boolean compare(char[] arr1, char[] arr2)
{
  List list = Arrays.asList(arr2);
  for(char c: arr1) //this is for-each loop in java
  {

    if(!list.contains(c)) //c isnt in arr2
    {
      //this character from arr1 isnt in arr2 
      return false;
    }
  }
//all characters from arr1 are in arr2
return true;
}
kajacx
  • 12,361
  • 5
  • 43
  • 70