Java one line solution #stream


  • 1

    The most efficient solution has been pointed out by other posts. This post is just to demo how we can solve this problem using Java stream API. The time and space complexity of this solution depends on how distinct() is implemented in the stream APIs and expects to be changed in the future.

    It would be a bonus point if you communicate with the interviewer that you are aware of this one line solution besides other more efficient ones.

    If I were in an interview, I would use the traditional Set to solve the problem while pointing out I know how to do it using stream.

        public boolean containsDuplicate(int[] nums) {
            return nums.length != IntStream.of(nums).distinct().count();
        }
    

  • 1
    H

    Good suggestion to gain some bonus points~


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.