일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- vuejs #vue #js #프론트엔드 #nodejs #클라이언트사이드 #템플릿엔진
- mybatis
- Dependency
- java
- springboot
- Collections
- 스프링
- Arrays
- pom.xml
- Spring
- HashMap
- @Scheduled
- thymeleaf
- pair
- GOF
- list
- Spring Mail
- 스프링 메일
- 스프링 부트
- 프로젝트 구조
- codility
- 스프링 스케줄러
- Spring Boot
- 스프링부트
- C++
- 의존성관리
- 코딩테스트
- maven
- spring scheduler
- 프로그래머스
- Today
- Total
Rooted In Develop
[Codility] Arrays - OddOccurrencesInArray / Java 본문
1. 문제
A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired.
For example, in array A such that:
A[0] = 9 A[1] = 3 A[2] = 9 A[3] = 3 A[4] = 9 A[5] = 7 A[6] = 9
- the elements at indexes 0 and 2 have value 9,
- the elements at indexes 1 and 3 have value 3,
- the elements at indexes 4 and 6 have value 9,
- the element at index 5 has value 7 and is unpaired.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A consisting of N integers fulfilling the above conditions, returns the value of the unpaired element.
For example, given array A such that:
A[0] = 9 A[1] = 3 A[2] = 9 A[3] = 3 A[4] = 9 A[5] = 7 A[6] = 9
the function should return 7, as explained in the example above.
Write an efficient algorithm for the following assumptions:
- N is an odd integer within the range [1..1,000,000];
- each element of array A is an integer within the range [1..1,000,000,000];
- all but one of the values in A occur an even number of times.
Copyright 2009–2020 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
2. 코드
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int result = 0;
Arrays.sort(A);
for(int i=0; i<A.length; i+=2) {
if(i+1 == A.length) {
result = A[i];
break;
}
if(A[i] != A[i+1]) {
result = A[i];
break;
}
}
return result;
}
}
'Algorithm Test > Java' 카테고리의 다른 글
[Codility] Time Complexity - PermMissingElem / Java (0) | 2020.06.14 |
---|---|
[Codility] Time Complexity - FrogJmp / Java (0) | 2020.06.14 |
[Codility] Arrays - CyclicRotation / Java (0) | 2020.06.14 |
[Codility] Iterations - BinaryGap / Java (0) | 2020.06.14 |
[프로그래머스] H-Index / Java (Arrays) (0) | 2020.05.06 |