# 먼저 반복되는 수 <div class="embed-responsive embed-responsive-16by9"> <iframe src="https://www.youtube.com/embed/44pfhb2VRhQ" frameborder="0" allowfullscreen></iframe> </div> ## 문제 다음 배열 중, 가장 먼저 반복되는 수를 찾아 출력하시오. ## 출력 예 ``` 배열: [3, 7, 12, 4, 8, 9, 12, 8] => 12 ```
관련 강의로 이동

코드: java 1.8

import java.util.*; class Main { // This function prints the first repeated // character in str[] static char firstRepeating(char str[]) { // Creates an empty hashset HashSet<Character> h = new HashSet<>(); // Traverse the input array from left to right for (int i=0; i<=str.length-1; i++) { char c = str[i]; // If element is already in hash set, update x // and then break if (h.contains(c)) return c; else // Else add element to hash set h.add(c); } return '\0'; } // Driver method to test above method public static void main (String[] args) { String str = "geeksforgeeks"; char[] arr = str.toCharArray(); System.out.println(firstRepeating(arr)); } }

입력

정답이 궁금하다면? 코드를 제출해보세요!