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 4 of 4

Thread: How to create a function that returns a new array containing duplicate values of another array?

  1. #1
    Junior Member
    Join Date
    Apr 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to create a function that returns a new array containing duplicate values of another array?

    I'm trying to do a problem that returns a new double array containing unique values from another double array (basically the new array will remove/delete duplicate numbers in the original array). I cannot use HashSets or ArrayLists in this problem. I tried using a brunt force method of swapping indexes from the original array and the new array I made. However, when there is only one element in an array, the function does not work. Below is my code. Example results, if this function is correct, include: new double [ ] {11, 22, 33, 44, 55, 66, 77, 88} < --removeDuplicates double [ ] {11, 11, 11, 11, 22, 33, 44, 44, 44, 44, 44, 55, 55, 66, 77, 88, 88}).

    public static double[] removeDuplicates (double[] list) {
     
    		int count = list.length - numDuplicates(list);
    		double [ ] newList = new double[count]; //numDuplicates returns the number of duplicated values in an array
    		int j = 0;
     
    		for (int i = 0; i < list.length - 1; i++) {
    			if (list[i] != list[i +1]) {
    				newList[j++] = list[i];
    			}
    			newList[j] = list[list.length - 1];
    		}
    		return newList;
    	}
    Attached Images Attached Images
    Last edited by Feeling_Supersonic; April 21st, 2019 at 02:42 AM.

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: How to create a function that returns a new array containing duplicate values of another array?

    1. Select next item from the list and add it to the new list.

    2. Now get next item and see if it is in new list.
    3. If no, add it
    4. if yes ignore,
    5. go back to 2 and repeat until done.

    Regards,
    Jim

  3. #3
    Junior Member
    Join Date
    Apr 2019
    Posts
    24
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: How to create a function that returns a new array containing duplicate values of another array?

    en.verejava.com/?id=19932689743040

    package com.tool.util;
     
    public class RemoveDuplicate {
     
        public static void main(String[] args) {
     
            double[] numArray = { 11, 11, 11, 11, 22, 33, 44, 44, 44, 44, 44, 55, 55, 66, 77, 88, 88 };
     
            double[] newArray = removeDuplicate(numArray);
     
            for (int i=0; i < newArray.length; i++) {
                System.out.print(newArray[i]+", ");
            }
     
        }
     
        public static double[] removeDuplicate(double[] arr) {
            int t = 0;
            double[] tempArr = new double[arr.length];
     
            for (int i = 0; i < arr.length; i++) {
                boolean isTrue = true;
                for (int j = i + 1; j < arr.length; j++) {
                    if (arr[i] == arr[j]) {
                        isTrue = false;
                        break;
                    }
                }
     
                if (isTrue) {
                    tempArr[t] = arr[i];
                    t++;
                }
            }
     
            double[] newArr = new double[t];
            System.arraycopy(tempArr, 0, newArr, 0, t);
            return newArr;
        }
    }

  4. #4
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    121
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: How to create a function that returns a new array containing duplicate values of another array?

    Quote Originally Posted by veretimothy View Post
    en.verejava.com/?id=19932689743040

    package com.tool.util;
     
    public class RemoveDuplicate {
     
        public static void main(String[] args) {
     
            double[] numArray = { 11, 11, 11, 11, 22, 33, 44, 44, 44, 44, 44, 55, 55, 66, 77, 88, 88 };
     
            double[] newArray = removeDuplicate(numArray);
     
            for (int i=0; i < newArray.length; i++) {
                System.out.print(newArray[i]+", ");
            }
     
        }
     
        public static double[] removeDuplicate(double[] arr) {
            int t = 0;
            double[] tempArr = new double[arr.length];
     
            for (int i = 0; i < arr.length; i++) {
                boolean isTrue = true;
                for (int j = i + 1; j < arr.length; j++) {
                    if (arr[i] == arr[j]) {
                        isTrue = false;
                        break;
                    }
                }
     
                if (isTrue) {
                    tempArr[t] = arr[i];
                    t++;
                }
            }
     
            double[] newArr = new double[t];
            System.arraycopy(tempArr, 0, newArr, 0, t);
            return newArr;
        }
    }
    This works but do you want doubles as a result or integers?


    Cheers.
    Last edited by MrLowBot; April 27th, 2019 at 01:33 AM.
    "Tick, tack"

Similar Threads

  1. How to create sets for array values
    By Meeeee55555 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 23rd, 2017, 11:59 AM
  2. how to find 2nd largest array if array values like{10,20,92,81,92,34}
    By anjijava16 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 2nd, 2013, 04:59 PM
  3. create an array of 99 random values; sorted and the printed
    By appoldk@yahoo.com in forum Java Theory & Questions
    Replies: 1
    Last Post: December 2nd, 2012, 04:57 PM
  4. Removing Duplicate Values From an Array
    By nicsa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 30th, 2011, 07:55 PM
  5. Array list returns null
    By Scotty in forum Collections and Generics
    Replies: 3
    Last Post: April 14th, 2011, 06:50 AM