codility OddOccurrencesInArray:
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.
class Solution {
public int solution(int[] A) {
final int len = A.length;
Arrays.sort(A);
for (int i = 0; i < len ;) {
int counter = 1;
int current = A[i];
while ((i < len - 1) && (current == A[++i])) {
counter++;
}
if (counter % 2 == 1) {
return current;
}
}
return -1;
}
}
Could this code be bettered in terms of time complexity or code quality?
codility OddOccurrencesInArray:
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.
class Solution {
public int solution(int[] A) {
final int len = A.length;
Arrays.sort(A);
for (int i = 0; i < len ;) {
int counter = 1;
int current = A[i];
while ((i < len - 1) && (current == A[++i])) {
counter++;
}
if (counter % 2 == 1) {
return current;
}
}
return -1;
}
}
Could this code be bettered in terms of time complexity or code quality?
codility OddOccurrencesInArray:
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.
class Solution {
public int solution(int[] A) {
final int len = A.length;
Arrays.sort(A);
for (int i = 0; i < len ;) {
int counter = 1;
int current = A[i];
while ((i < len - 1) && (current == A[++i])) {
counter++;
}
if (counter % 2 == 1) {
return current;
}
}
return -1;
}
}
Could this code be bettered in terms of time complexity or code quality?
- 7.4k
- 3
- 21
- 55
Finding unpaired number in an odd length Array of integers
codility OddOccurrencesInArray :
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] A[0] = 9 A[1] A[1] = 3 A[2] A[2] = 9
A[3] = 3 A[4] A[4] = 9 A[5] A[5] = 7A[6] = 9
the• the elements at indexes 0 and 2 have value 9,
the• the elements at indexes 1 and 3 have value 3,
the• the elements at indexes 4 and 6 have value 9,
the• 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.
class Solution {
public int solution(int[] A) {
final int len = A.length;
Arrays.sort(A);
for (int i = 0; i < len ;) {
int counter = 1;
int current = A[i];
while ((i < len - 1) && (current == A[++i])) {
counter++;
}
if (counter % 2 == 1) {
return current;
}
}
return -1;
}
}
Could this code be bettered in terms of time complexity ofor code quality?
Finding unpaired number in an odd Array of integers
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] = 7A[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.
class Solution {
public int solution(int[] A) {
final int len = A.length;
Arrays.sort(A);
for (int i = 0; i < len ;) {
int counter = 1;
int current = A[i];
while ((i < len - 1) && (current == A[++i])) {
counter++;
}
if (counter % 2 == 1) {
return current;
}
}
return -1;
}
}
Could this code be bettered in terms of time complexity of code quality?
Finding unpaired number in an odd length Array of integers
codility OddOccurrencesInArray :
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] = 7A[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.
class Solution {
public int solution(int[] A) {
final int len = A.length;
Arrays.sort(A);
for (int i = 0; i < len ;) {
int counter = 1;
int current = A[i];
while ((i < len - 1) && (current == A[++i])) {
counter++;
}
if (counter % 2 == 1) {
return current;
}
}
return -1;
}
}
Could this code be bettered in terms of time complexity or code quality?
- 10.7k
- 5
- 37
- 101
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.
class Solution {
public int solution(int[] A) {
final int len = A.length;
Arrays.sort(A);
for (int i = 0; i < len ;) {
int counter = 1;
int current = A[i];
while ((i < len - 1) && (current == A[++i])) {
counter++;
}
if (counter % 2 == 1) {
return current;
}
}
return -1;
}
}
Could this code be bettered in terms of time complexity of code quality? Please forgive the bad indentation.
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.
class Solution {
public int solution(int[] A) {
final int len = A.length;
Arrays.sort(A);
for (int i = 0; i < len ;) {
int counter = 1;
int current = A[i];
while ((i < len - 1) && (current == A[++i])) {
counter++;
}
if (counter % 2 == 1) {
return current;
}
}
return -1;
}
}
Could this code be bettered in terms of time complexity of code quality? Please forgive the bad indentation.
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.
class Solution {
public int solution(int[] A) {
final int len = A.length;
Arrays.sort(A);
for (int i = 0; i < len ;) {
int counter = 1;
int current = A[i];
while ((i < len - 1) && (current == A[++i])) {
counter++;
}
if (counter % 2 == 1) {
return current;
}
}
return -1;
}
}
Could this code be bettered in terms of time complexity of code quality?