3

How should I proceed to turn this piece of code into pseudocode?

ArrayList<Integer> check = new ArrayList<Integer>();
ArrayList<Integer> dup = new ArrayList <Integer> ();
ArrayList<Integer> nonDup = new ArrayList <Integer> ();

for (int i : listA) {
    nonDup.add(i);
}
for (int i : listB) {
    nonDup.add(i);
}
for (int i : listA) {
    check.add(i);
}
for (int i : listB) {
    if (check.contains(i)) {
        dup.add(i);
        nonDup.removeAll(duplicates);                   
    }
}

I have no idea how to turn the for loops, add(), contains() and removeAll() methods into pseudocode.

5
  • 1
    Pseudocode is just code in plain English. So just write what the functions do. For example 'remove all duplicates from nonDup'. Commented Jan 4, 2014 at 16:55
  • Usually one goes from pseudocode to code... Commented Jan 4, 2014 at 16:59
  • 2
    @Drew must be homework :)) Commented Jan 4, 2014 at 17:00
  • Yes it is (: I got the code done beforehand, cause I find coding fun but pseudocode not that much. I'm sure I'll learn to adapt in the future though. Commented Jan 4, 2014 at 17:04
  • 1
    @user3116280 see my answer :) Normally you don't need to bother with pseudocodes. They really don't have much use (other than bragging rights, I mean, helping your peers understand your code) and they are not executable. Commented Jan 4, 2014 at 17:12

4 Answers 4

5

PseudoCode can be whatever you want. Since you or other people can understand what the lines mean.

You can turn it simple as FOR (your variable value start) TO (your end desired) i++ -

Basically something that makes people and you mostly understand that it is a For Loop

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I can understand how I can turn the functions into pseudocode now. But what about for (int i : listA) { This piece turns the integer i into each number inside the array listA What would you suggest for this? Thanks
No problem :) just looking to help . if you want you can accept my answer so that other users that might have the same question may find it easier
4

It's just plain English:

Initialize "check" as an empty (array-backed) list of integers.
Initialize "dup" as an empty (array-backed) list of integers.
Initialize "nonDup" as an empty (array-backed) list of integers.

For each integer in listA:
    Add the integer to "nonDup".
End of loop.

For each integer in listB:
    Add the integer to "nonDup".
End of loop.

For each integer in listA:
    Add the integer to "check".
End of loop.

For each integer in listB:
    If "check" contains the integer:
        Add the integer to "dup".
        Remove all integers in "dup" from "nonDup".
    End of if.
End of loop.

Normally you don't need to bother with pseudocodes. They really don't have much use (other than bragging rights... I mean, helping your peers understand your code) and they are not executable.

Comments

4

For example:

getNonDup(listA, listB):
    nonDup = listA + listB
    dup    = an empty list
    for each object i in listB do:
        if listA contains i do:
            add i to dup
            remove i from nonDup
    return nonDup

(my pseudocode style is somehow similar to Python...)

In Java, to have only unique values, you can simply put all of them in a set:

Set<Integer> nonDup = new HashSet<Integer>(listA.addAll(listB));

Comments

3

How about something like this:

for each element in list A
    add element to list nonDup

Its basically just plain text which can be read by anyone. You could choose more speaking names for the variables. You could also choose begin loop and end loop to show the scope of the loop instead of indentication.

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.