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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Identify maximum distance element from String array

  1. #1
    Junior Member
    Join Date
    Nov 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Identify maximum distance element from String array

    I have a requirement where I have to find an element from an array that has maximum distance in occurrence.

    If there are two terms that were equally searched, the return term has the longest distance between when it was first and last searched.
    Given the following array
    [‘C++’, ‘Java’, ‘C#’, ‘C#’, ‘Java’, ‘Python’, ‘C#’, ‘Java’]
    Our Program should return “Most searched term is Java”

    Note: Both ‘Java’ and ‘C#’ were searched 3 times, but the program should return ‘Java’ as distance
    between its first and last search is greater than that of ‘C#’.

    I tried searching google, I can find the distance (6) but couldn't return the value (Java).

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Identify maximum distance element from String array

    element from an array that has maximum distance in occurrence.
    Can you post an algorithm for searching an array to solve it?
    Once you post an algorithm, we can help you write the java code to implement the algorithm.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Identify maximum distance element from String array

    With this code, I can find the difference in index position but couldn't catch the value.


    public class mstry {

    public static void main (String[] args){
    String[] a ={"C++", "Java", "C#", "C#", "Java", "Python", "C#", "Java"};

    int maxDist = 0;
    for(int i=0; i<a.length; i++)
    {
    int firstOcc = -1;
    int lastOcc = -1;

    for(int j=0; j<a.length; j++)
    {
    if(a[i] == a[j])
    {
    if(firstOcc == -1)
    firstOcc = lastOcc = j;
    else
    lastOcc = j;
    }
    }
    if(lastOcc - firstOcc > maxDist)
    maxDist = lastOcc - firstOcc;
    }
    System.out.println(maxDist);
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Identify maximum distance element from String array

    couldn't catch the value.
    Can you describe the steps you take to find the value when you do it manually?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    http://www.java-forums.org/misc.php?do=bbcode#code
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Identify maximum distance element from String array

    Array = [‘C++’, ‘Java’, ‘C#’, ‘C#’, ‘Java’, ‘Python’, ‘C#’, ‘Java’]

    Steps:

    1. Find the maximum occurrence of the element; in this case, we have 2 elements Java and C# they have maximum occurrence 3
    2. Then, find 1st and last occurrence of the elements (Java and C#) position
    3. Now subtract the first occurrence index from the last occurrence index for Java, same for C#
    4. Print the max difference which is 6 for Java.

    Here, I want to print Java.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Identify maximum distance element from String array

    Some questions about holes I see in the above"
    Where are the elements (Java and C#) saved for using in step 2?
    How does step 1 know what to save?
    How are the distances for each saved element saved so they can be used for step 4?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] boolean array maximum size
    By dicdic in forum Java Theory & Questions
    Replies: 4
    Last Post: October 31st, 2013, 12:47 PM
  2. Finding and printing maximum value in 2D array..right section? :\
    By new2.java in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 6th, 2013, 04:56 PM
  3. Maximum length of a string
    By ranjithfs1 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 6th, 2012, 09:47 AM
  4. Replies: 1
    Last Post: September 9th, 2011, 07:21 AM
  5. problem searching for a String element in a parallel array
    By david185000 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 27th, 2010, 01:24 PM

Tags for this Thread