Answer the question
In order to leave comments, you need to log in
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
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.
It is typed like this:
If the text for semantics is above the text - svg
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.
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.
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.
Map<Integer, Integer> (Map<Число, Кол-во>)
int numIndex = arr.length - 1
, and the number itself can be found through int num = arr[numIndex]
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,
If there is no such element (all array elements are different), then return -1.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question