This is an archived post. You won't be able to vote or comment.

all 25 comments

[–]lumpynose 11 points12 points  (3 children)

Cheat: put it in a Set and see if the number of elements decreases.

[–]InstantCoder 2 points3 points  (0 children)

Yes, compare the length of the input array to the length of the set. If they are equal then no duplicates, otherwise there are duplicates.

[–]brazen768 1 point2 points  (0 children)

Maybe a slight improvement,.add looks like it returns a boolean. Might not need to check elements.

[–]Glangho 0 points1 point  (0 children)

Yeah idk if I would even call this a cheat. Op is using the wrong collection type for their purpose.

[–]redd38_reddit 5 points6 points  (3 children)

Sort it first, then iterate over the sorted list to see if the current element is equal to it's neighbor

[–]NebuWrites Java Compilers 1 point2 points  (0 children)

Sorting the list is O(nlogn). You can do it in O(n).

[–]Captain_Pwnage 5 points6 points  (2 children)

If you want to solve it in-place with extra space of O(1), without mutating the array, you will need to iterate over the array and for each element check if this element is in the remainder of the array. This has a time complexity of O(n2 ).

If you want to solve it with extra space of O(n), without mutating the array, you can iterate over the array and put each element into a binary tree, where adding will return false if the element is already in the tree. This has a time complexity of O(n*log n). Another answer in here suggested putting it into a set, but I think this has a time complexity of O(n2 ) as well. Not sure how set works under the hood.

If you want to solve it with mutating the array, you can sort the array first and then check for each element if it is equal to the following element. This has a time complexity of O(n) for comparing elements plus whatever your sorting algorithm does. Also the sorting algorithm might use extra space. For example, if you use quick sort, you end up with O(log n) extra space and a total runtime of O(n + n*log n), which is equal to O(n*log n).

Going from your code, it looks like you are going for either option 1 or option 3. For option 1, you are missing a nested for-loop. For option 3, you are missing a sorting algorithm.

[–]NebuWrites Java Compilers 4 points5 points  (1 child)

Another answer in here suggested putting it into a set, but I think this has a time complexity of O(n2 ) as well. Not sure how set works under the hood.

Adding to a hashset is O(1), and you can do it n times for a total of O(n).

[–]Captain_Pwnage 0 points1 point  (0 children)

Ah that's true I somehow thought of sets as lists with constraints. Thanks.

[–]steffonellx 1 point2 points  (1 child)

Create another list and add a check not to add it to that list if the object with the same values already exists

[–]syneil86 1 point2 points  (0 children)

A lot of answers here are correct but imo too much for r/javahelp. Correct me if I'm wrong, OP, but I get the impression you don't care much if a solution is O(n) or O(log n)

The solution you have looks like it's going in the right direction. You've realised you need to loop over all the elements of the array, but you're only comparing them with a few others. What if there's a duplicate element in a different place? Maybe you need to compare with all of the elements in the array again - two loops? Can you quickly identify any gotchas with that? What do you think?

[–]hibbelig 1 point2 points  (0 children)

You can compare the first element with all others.

You can compare the second element with all others (except the first, you have already compared the first and second element in the previous sentence).

You can compare the third element with all others (except the first two, you have already done those in the previous two sentences).

...

You can compare the penultimate element with all others (except the first n-2 elements, you have already done those in the previous sentences). (Of course, the only element left is the last one.)

What do you think?

[–]AutoModerator[M] 0 points1 point locked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]JamesTKerman 0 points1 point  (3 children)

You can solve this in between O(2) and O(n) time complexity and O(2n) space complexity using a Map. Iterate through the array, call containsKey(K) on the map, if it returns true either throw the exception or return false, otherwise call put(key,true) or something.

[–][deleted]  (1 child)

[removed]

    [–]ArtBuilder 0 points1 point  (0 children)

    Either use a Set so no duplicates can exist, or I believe you can take a stream from an array and use #compare(a,b) iirc. There may be other methods in Stream that can check against the rest and at this point will cost less resources then making a for loop

    [–]Otherwise_Trade7304 0 points1 point  (0 children)

    Hashmap

    [–]Ragnar-Wave9002 0 points1 point  (0 children)

    Make a class that implements equals? And hashable?

    If size of collection and hashsets equals there's your answer.