1

So I am trying to compare two char arrays, and all letters that are present in keyW, should be removed from the array invAlphabet. I have been able to find the duplicates inside the invAlphabet array, however, I don't know how to remove the duplicate elements without using Lists or Collections which I should not use... Any idea?

public static void main(String[] args)
{
    final int SIZE = 26;
    char[] keyW = {'A', 'L','O'};

    char[] invAlphabet = new char [SIZE];


    for (int i = 0; i < SIZE; i++)
    {
        invAlphabet[i] = (char)('Z' - i);

    }



    for (int i = 0; i<keyW.length; i++)
    {
        for (int j = 0; j < invAlphabet.length; j++)
        {
            if(keyW[i] == invAlphabet[j])
            {
                //need to delete the invAlphabet[j] elements that are duplicates
                System.out.println(invAlphabet[j]);
                System.out.println(j);
                break;

            }

        }
    }

}
3
  • 2
    What do you mean by "remove" in the context of an array - you can't resize arrays. Do you mean to create a new array with only certain elements in it? Do you need to preserve ordering? Commented Nov 26, 2016 at 21:16
  • (If you don't need to preserve ordering, sort the two arrays and just iterate through them, looking for common elements). Commented Nov 26, 2016 at 21:17
  • @AndyTurner Yes. I want to create a new invAlphabet but without the elements that are present in the keyW array. invAlphabet is a sorted array that contains the alphabet letters inverted (Z-A). So after taking out the A, L and O letter, the new invAlphabet array should be in the order Z-B without A , L or O present. Commented Nov 26, 2016 at 21:21

4 Answers 4

1

If you want to solve it in O(n), then you can mark all the character That are present in keyW[] array, and then check and don't add them to your new noDuplicateArray[].

char[] keyW = {'A', 'L', 'O', 'P'};
char[] invAlphabet = {'X', 'A', 'P', 'B', 'C'};

//create boolean array
boolean[] mark = new boolean[128];
Arrays.fill(mark, false);

//mark which characters are present in keyW array
for (char ch : keyW) {
    mark[ch] = true;
}

// find number of duplicate character in invAlphabet array
int duplicateCount = 0;
for (char ch : invAlphabet) {
    if (mark[ch]) {
        duplicateCount++;
    }
}

// create new array
// size of new array = invAlphabet array length - duplicate number of character in invAlphabet array
char[] noDuplicateArray = new char[invAlphabet.length - duplicateCount];

//add character in new array
int idx = 0;
for (char ch : invAlphabet) {
    if (!mark[ch]) {
        noDuplicateArray[idx++] = ch;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nice solution, O(n)!
0

You cannot resize the array object, as you see you could use some other data types. However, if you are allowed to just use array, you can put some other non-alphabetic character in place of removed character. For example '0'. So while using or printing you can skip the character in the array if it is '0'.

Comments

0

Could you use String and replace method?

String invAlphabetString = new String(invAlphabet);
for(char i:keyW){
    invAlphabetString=invAlphabetString.replace(""+i, "");
}
char[] invAlphabetWithoutKeyW = invAlphabetString.toCharArray();
System.out.println(Arrays.toString(invAlphabetWithoutKeyW));

Comments

0

I would begin by writing a method to search a char[] for a given char (that is, a contains method) like

private static boolean contains(char[] chars, char ch) {
    for (char c : chars) {
        if (c == ch) {
            return true;
        }
    }
    return false;
}

Then the problem can be decomposed into two steps. First, count the duplicates and second build an output array by copying without the duplicates. Something like

int dupes = 0;
for (char ch : invAlphabet) {
    if (contains(keyW, ch)) {
        dupes++;
    }
}
int i = 0;
char[] noDupes = new char[invAlphabet.length - dupes];
for (char ch : invAlphabet) {
    if (!contains(keyW, ch)) {
        noDupes[i] = ch;
        i++;
    }
} 

Alternatively, you could convert your keyW array to a String. And, in Java 8+, you could construct a Stream of your characters. Map to the array, filter against the String and then collect to another String. Something like,

String keyWord = new String(keyW);
char[] noDupes = IntStream.range(0, invAlphabet.length)
        .mapToObj(x -> invAlphabet[x])
        .filter(ch -> (keyWord.indexOf(ch) < 0))
        .map(String::valueOf)
        .collect(Collectors.joining()).toCharArray();

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.