Lesson 4_Counting Elements_PermCheck

Lesson 4_Counting Elements_PermCheck

https://app.codility.com/programmers/lessons/4-counting_elements/perm_check/

import java.util.HashSet; class Solution { public int solution(int[] A) { HashSet set = new HashSet<>(); int maxNum = 0; int sum = 0; for (int i : A) { if (set.contains(i)) { return 0; }else { set.add(i); } maxNum = Math.max(maxNum, i); sum += i; } int totalSum = (maxNum*(maxNum+1)) / 2; return (totalSum == sum)? 1 : 0; } }

from http://horned-holly.tistory.com/9 by ccl(A) rewrite - 2021-12-15 13:28:07