N
N
ninetiledfox2021-04-26 20:20:54
css
ninetiledfox, 2021-04-26 20:20:54

Find the position of an element in an array?

Hello, gentlemen, please help me, I've been racking my brains for a day, but I just can't figure out how to write the code for this problem correctly.

An array of integers is entered. Find the position of the second element from the end that occurs most often in the array. If several values ​​occur in the array the same number of times, select the largest by absolute value, then simply by value. If there is no such element (all array elements are different), then return -1.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
I
Ivan Bogachev, 2018-08-30
@sfi0zy

Two large pictures with object-fit: cover. One is normal, the second is only the head and paw of an elephant, and nothing is transparent around it. The head is in the center, with an adaptive change in the proportions of all this, it will not go anywhere. Pictures are the same size. Then there are three layers with absolute positioning - a normal image, a layer with text and an image with a head. The text is in the form of an embedded SVG, where it will be possible to apply any gradients on it cross-browser.

M
Maxim Timofeev, 2018-08-31
@webinar

It is typed like this:
If the text for semantics is above the text - svg

E
Egor Danchenko, 2018-08-30
@Danchenko

In simple words - SVG

W
Wataru, 2021-04-26
@wataru

1) Find the element that occurs most often. To do this, you can count for each element in the array how many times it occurs in a nested loop, or it is better to use some kind of hashmap to store counters. Or sorting a copy of the array and counting the number of occurrences there is already very easy.
2) Find the second element from the end. First, if the most frequent occurs only 1 time, then there is no answer (-1 by condition). If it occurs 2 or more times, then go from the end of the array and count how many times you encountered elements equal to this one. When you count to two, you have found the answer.

O
Orkhan, 2021-04-27
Hasanly @azerphoenix

Since this is a task, you need to solve it yourself, but I can suggest approximate steps for solving this task:

An array of integers is entered.

1) Using the Scanner class, we get integers (int) as input and add them to the array. For convenience, you can first add them to the List, and then get an Array from the List, since the array is of a fixed size during initialization and then you will have to recreate the array each time. It is easier when entering a number from the console to add them to the list List<Integer>, and then get an array of numbers from the list.
Find the position of the second element from the end that occurs most often in the array.

2) To do this, first you need to create Map<Integer, Integer> (Map<Число, Кол-во>)
The second element from the end of the array is found using int numIndex = arr.length - 1, and the number itself can be found through int num = arr[numIndex]
If this number is in the array more than 2 times, then from the end of the array you need to calculate the occurrence of elements equal to this element. When you reach 2 it will be your answer to the problem.
If you need to find the most frequently occurring element, then you can try to create Map<Integer, Integer>key = number stored in the array value (count) its number (how many times it occurs in the array).
If several values ​​occur in the array the same number of times, choose the largest by absolute value,

3) If value (count) from Map matches another value, then select the largest key value.
Those. let's say in Map you have the number 4 occurs 10 times and the number 5 occurs 11 times, then select the number 5.
If there is no such element (all array elements are different), then return -1.

4) If there are no identical numbers in the array, then we return -1
Approximately, something like this ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question