Here's a version based on the hint in the description. freqs is a sequence of letter frequency maps, one for each column. There should be 6 of each letter in each column, so we look for the one with 5.

# We won't validate every precondition, but we do have some basic# guards.if arr.length == 0throw Error "Need more data"if arr.length == 1return[arr[0][1]+ arr[0][0]]

# Now we know that for each position in the string, elements should appear# an even number of times (N-1 >= 2). We can use a set to detect the element appearing# an odd number of times. Detect odd occurrences by toggling admission/expulsion# to and from the set for each value encountered. At the end of each pass one element# will remain in the set. result = ''for pos in[0...arr[0].length] set = {}for permutation in arr c = permutation[pos]if set[c]delete set[c]else set[c] = truefor c of set result += cbreak result

FOR I%=1 TO N DO CH$=CHR$(I%+64) COUNT%=0 FOR Z%=1 TO N DO COUNT%=0 FOR J%=1 TO UBOUND(PERMS$,1) DO IF CH$=MID$(PERMS$[J%],Z%,1) THEN COUNT%=COUNT%+1 END IF END FOR IF COUNT%<>6 THEN !$RCODE="MID$(SOL$,Z%,1)=CH$" END IF END FOR END FOR PRINT("Solution is: ";SOL$)END PROGRAM

The approach above generates a full set of permutations and calculates the difference. Changing the two commented lines to the three below will calculate on the fly and would be more efficient for larger data sets.

every x := permutes("ABCD")do# generate all permutations ifmember(givens,x)thendelete(givens,x)# remove givens as they are generatedelseinsert(givens,x)# add back any not given

However, that's actually a false economy - not only does this approach take more code to implement (at least, in J) but we are already dealing with a data structure of approximately the size of all permutations. So what is being saved by this supposedly "more efficient" approach? Not much... (Still, perhaps this exercise is useful as an illustration of some kind of advertising concept?)

# Input: an array of integers (based on the encoding of A=0, B=1, etc)# corresponding to the occurrences in any one position of the# letters in the list of permutations.# Output: a tally in the form of an array recording in position i the# parity of the number of occurrences of the letter corresponding to i.# Example: given [0,1,0,1,2], the array of counts of 0, 1, and 2 is [2, 2, 1],# and thus the final result is [0, 0, 1].def parities: reduce .[] as $x ( []; .[$x] = (1 + .[$x]) % 2);

This solution is designed to work on a column vector of strings. This will not work with a cell array or row vector of strings.

function perm = findMissingPerms(list)

permsList = perms(list(1,:)); %Generate all permutations of the 4 letters perm = []; %This is the functions return value if the list is not missing a permutation

%Normally the rest of this would be vectorized, but because this is%done on a vector of strings, the vectorized functions will only access%one character at a time. So, in order for this to work we have to use%loops.fori = (1:size(permsList,1))

Alternate method : if we had all permutations,
each letter would appear an even number of times at each position.
Since there is only one permutation missing,
we can find where each letter goes by looking at the parity
of the number of occurences of each letter.
The following program works with permutations of at least 3 letters:

-- 1: sum of letterssequence r = repeat(0,4)for i=1 to length(perms) do r = sq_add(r,perms[i])end forr = sq_sub(max(r)+'A',r)puts(1,r&'\n')-- based on the notion that missing = sum(full)-sum(partial) would be true,-- and that sum(full) would be like {M,M,M,M} rather than a mix of numbers.-- the final step is equivalent to eg {1528,1530,1531,1529} -- max-r[i] -> { 3, 1, 0, 2}-- to chars -> { D, B, A, C}-- (but obviously both done in one line)

-- 2: the xor trickr = repeat(0,4)for i=1 to length(perms) do r = sq_xor_bits(r,perms[i])end forputs(1,r&'\n')-- (relies on the missing chars being present an odd number of times, non-missing chars an even number of times)

-- 3: find least frequent lettersr = " "for i=1 to length(r) do sequence count = repeat(0,4) for j=1 to length(perms) do count[perms[j][i]-'A'+1] += 1 end for r[i] = smallest(count,1)+'A'-1end forputs(1,r&'\n')-- (relies on the assumption that a full set would have each letter occurring the same number of times in each position)-- (smallest(count,1) returns the index position of the smallest, rather than it's value)

-- 4: test all permutationsfor i=1 to factorial(4) do r = permute(i,"ABCD") if not find(r,perms) then exit end ifend forputs(1,r&'\n')-- (relies on brute force(!) - but this is the only method that could be made to cope with >1 omission)

Here is a solution that is more in the spirit of the challenge,
i.e. it never needs to generate the full set of expected permutations.

def missing_permutation(arr):"Find the missing permutation in an array of N! - 1 permutations."

# We won't validate every precondition, but we do have some basic# guards.iflen(arr)==0: raiseException("Need more data")iflen(arr)==1:return[arr[0][1] + arr[0][0]]

# Now we know that for each position in the string, elements should appear# an even number of times (N-1 >= 2). We can use a set to detect the element appearing# an odd number of times. Detect odd occurrences by toggling admission/expulsion# to and from the set for each value encountered. At the end of each pass one element# will remain in the set. missing_permutation =''for pos inrange(len(arr[0])): s =set()for permutation in arr: c = permutation[pos]if c in s: s.remove(c)else: s.add(c) missing_permutation +=list(s)[0]return missing_permutation

It is rather obfuscated, but can be explained
by showing these intermediate results and noting
that zip(*x) transposes x;
and that at the end of the list
created by the call to most_common()
is the least common character.

for a = ascii("A") to ascii("D") for b = ascii("A") to ascii("D") for c = ascii("A") to ascii("D") for d = ascii("A") to ascii("D") x = char(a) + char(b) + char(c)+ char(d) if a!=b and a!=c and a!=d and b!=c and b!=d and c!=d if substr(list,x) = 0 see x + " missing" + nl ok ok next next next next

for a = asc("A") to asc("D") for b = asc("A") to asc("D") for c = asc("A") to asc("D") for d = asc("A") to asc("D") x$ = chr$(a) + chr$(b) + chr$(c)+ chr$(d) for i = 1 to 4 ' make sure each letter is unique j = instr(x$,mid$(x$,i,1)) if instr(x$,mid$(x$,i,1),j + 1) <> 0 then goto [nxt] next i if instr(list$,x$) = 0 then print x$;" missing" ' found missing permutation[nxt] next d next c next bnext a