简体   繁体   中英

Printing Char Elements Without Duplicates

i have this char array

char[] c = new char[]{'h','e','l','l','o','w','o','r','l','d';

i would like to look for duplicate elements in the array and print the elements in the array only once like so:

h,e,l,o,w,r,d

i tried with this code

for (int j = 0; j < c.length; j++) {

                    if (c[j] == c[j]) {

                        System.out.println("Duplicate");

                    } }

how can i achieve this?

Try it using Java 8: Arrays.asList(c).stream().distinct().collect(Collectors.toList()); to get the array back try: Arrays.asList(c).stream().distinct().toArray(size -> new Char[size]); Its all untested so Plesse let me know if these is something mit working. For printing add .foreach(System.out::println)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM