Java Brush Question (v)

Source: Internet
Author: User

Codewars Brush question fifth day, array problem.

Enough is enough!

Alice and Bob were on a holiday. Both of them took many pictures of the places they ' ve been,

And now they want to show Charlie their entire collection.

However, Charlie doesn ' t like this sessions, since the motive usually repeats.

He isn ' t fond of seeing the Eiffel tower.

He tells them that he'll only sit during the session if they show the same motive at most N times.

Luckily, Alice and Bob is able to encode the motive as a number.

Can them to remove numbers such this their list contains each number is only up to N times,

Without changing the order?

Task

Given a list lst and a number N,

Create a new list that contains each number of the LST at the most N times without reordering.

For example if N = 2, and the input is [1,2,3,1,2,1,2,3],

You take [1,2,3,1,2], drop the next [up] since this would leads to 1 and 2 being in the result 3 times,

And then take 3, which leads to [1,2,3,1,2,3].

Example

Enoughisenough.deletenth (new int[] {20,37,20,21}, 1)//return [20,37,21]

Enoughisenough.deletenth (new int[] {1,1,3,3,7,2,2,2,2}, 3)//return [1, 1, 3, 3, 7, 2, 2, 2]

Title Requirements:

Given an array, the number of occurrences in the array must not exceed the limit given the limit maxoccurrences. The array is then returned,

For this indeterminate length problem, it is recommended to use a vector (), or a hash table.

Here, two functions are used to compare ingenious Getordefault (int,int) and Arrays.copyofrange (int,int,int). It's a good solution to the problem in traversal.

Java Brush Question (v)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.