Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## java.math help?

Hi, I'm VERY new to Java, and I did a test earlier today,

I was unable to do it as I didn't know where to begin!

I was wondering if anyone was able to assist me with this or point me in the right direction:

Write a function:

class Solution { public int solution(int[] A); }

that, given a non-empty zero-indexed array A of N integers, returns the first unique number in A. In other words, find the unique number with the lowest position in A. The function should return −1 if there are no unique numbers in A.

For example, given:
A[0] = 1
A[1] = 4
A[2] = 3
A[3] = 3
A[4] = 1
A[5] = 2
the function should return 4.

Given array A such that:
A[0] = 6
A[1] = 4
A[2] = 4
A[3] = 6

the function should return −1.

Assume that:

N is an integer within the range [1..100,000];
each element of array A is an integer within the range [0..1,000,000,000].

Complexity:

expected worst-case time complexity is O(N*log(N));
expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.

2. ## Re: java.math help?

Originally Posted by Bump247
that, given a non-empty zero-indexed array A of N integers, returns the first unique number in A. In other words, find the unique number with the lowest position in A. The function should return −1 if there are no unique numbers in A.
The most basic and straightforward approach: a double nested for loop, the external loop to scan each number and the internal loop to check if that number repeats (it's sufficient one another) in successive elements.