My input are{ AD,AF,DF,BE,BG,BK,EG,EK,GK,CH,CI,J}

java

#1

My input are{ AD,AF,DF,BE,BG,BK,EG,EK,GK,CH,CI,J} I want to write a java code and make the output like:

ADF

BEGK

CHI

J

Thanks a lot!


#2

Is it necessary to break up the letters in the grouping above?

We can see that the order must be preserved of first occurrences. Here is a Python mock up that preserves order. It should offer somewhat of a road map toward your Java solution.

start = ['AD', 'AF', 'DF', 'BE', 'BG', 'BK', 'EG', 'EK', 'GK', 'CH', 'CI', 'J']

singles = []

for i in ''.join(start):
    if i not in singles:
        singles.append(i)

print ' '.join(singles)

# A D F B E G K C H I J

If the groupings are necessary,

print ''.join(singles[:3])
print ''.join(singles[3:7])
print ''.join(singles[7:10])
print ''.join(singles[10])

'''
ADF
BEGK
CHI
J
'''

#3

Hi Roy,

Thanks a lot for your help. But I have to use java to write it. Can you
help me with that? Thanks a lot.

Best regards,
Bingyi


#4

I’m not versed in Java which is why I gave you a Python version to port over. The logic is all there, just put it into statements that Java can execute.


#5

JavaScript is pretty close to Java in terms of syntax, stemming from the same roots… C. It’s come a ways over the last decade of so, and earned a place among the respected programming environments it only looked up to a decade ago.

 > const bInA = (a, b) => a.some(x => x == b)
<- undefined
 > let y = 'a'
<- 'a'
 > bInA(['d', 'c', 'b','a'],  y)
<- true
> const bNotInA = (a, b) => ! bInA(a, b)
<- undefined
 > bNotInA(['d', 'c', 'b','a'],  y)
<- false