Responsive Advertisement

How to find duplicate elements in array in effective way. I mean to say with very less iterations

 

How to find duplicate elements in array in effective way. I mean to say with very less iterations


<?php

    Using Characters hereas give more flexibility.

    Character[] arrayC= (new Character[] { 'a''a''a''b''b' ,'v'});
    List<Character> allChars= new ArrayList<>(Arrays.asList(arrayC));
    List<Character> duplicates = allChars.stream().distinct().filter(
                                entry -> Collections.frequency(allCharsentry> 1).
                                collect(Collectors.toList());

    // duplicates  --> 'a','b'
    First the array is converted into an ArrayList. Thenif finds some entry that is repeated more than onceit collects it into duplicates.

    If you want to get which are not duplicatesjust:

    allChars.removeAll(duplicates);

Post a Comment

0 Comments