Java + Count duplicates from int array without using any Collection or another intermediate Array

前端 未结 10 1376
面向向阳花
面向向阳花 2020-12-06 02:50

As a part of the Java interview question paper I have got following issue to solve. But I am bit wonder whether how can I implement it without any Collection or intermediate

相关标签:
10条回答
  • Keeping one extra variable for maintaining count, plus sorting of array in the initial phase.

    public static void main(String[] args) {
            int[] numbers = { 7, 2, 6, 1, 4, 7, 4, 5, 4, 7, 7, 3, 1 };
            Arrays.sort(numbers);
            System.out.println("Sorted Array is :: = " + Arrays.toString(numbers));
    
            int count = 0;
            int tempCount = 0; // to keep local count of matched numbers
            String duplicates = "";
            for (int i = 1; i < numbers.length; i++) {
                if (numbers[i] == numbers[i - 1]) {
                    if ((tempCount == 0)) { // If same number is repeated more than
                                            // two times, like 444, 7777
                        count = count + 1;
                        tempCount = tempCount + 1;
                        duplicates = duplicates.concat(Integer.toString(numbers[i])
                                + ",");
                    }
                } else {
                    tempCount = 0;
                }
            }
    
            System.out.println("No of duplicates :: = " + count);
            System.out.println("Duplicate Numbers are :: = " + duplicates);
        }
    

    output

    Sorted Array is :: = [1, 1, 2, 3, 4, 4, 4, 5, 6, 7, 7, 7, 7]
    No of duplicates :: = 3
    Duplicate Numbers are :: = 1,4,7,
    
    0 讨论(0)
  • 2020-12-06 03:41

    This is the simplest solution I can think of. I just added an extra counter so that integers with two or more repetitions still in the array are ignored.

    static int findNumber(int[] arr) 
    {  
        int duplicateCounter = 0;
    
        System.out.print("Duplicates: ");
    
        for(int i = 0; i < arr.length; i++)
        {
            boolean duplicate = false;
            int numOfOccurrences = 1;
    
            for (int j = (i+1); j < arr.length; j++)
            {
                if (arr[i] == arr[j])
                {
                    numOfOccurrences++;
                    duplicate = true;
                }
            }
            if(numOfOccurrences == 2 && duplicate == true)
            {
                duplicateCounter++;
                System.out.print(arr[i] + " ");
            }
        }
    
        return duplicateCounter;
    }
    

    My test run: Test run

    Input: 1, 2, 3, 4, 2, 4, 1, 1, 1

    Duplicates: 2 4 1

    Number of duplicates: 3

    0 讨论(0)
  • 2020-12-06 03:41

    Below method not use any collection, just use Arrays.sort() method to help sort array into ascending order as default, e.g array = [9,3,9,3,9] will sort into [3,3,9,9,9].If input [9,9,9,9,9], expected result is 1, since only repeated number is 9.If input [9,3,9,3,9,255,255,1], expected result is 3, since repeated numbers are 3,9,255. If input [7,2,6,1,4,7,4,5,4,7,7,3,1], expected result is 3, since repeated numbers are 1,4,7.

    public static int findDuplicateCountsInArray(int[] nums) {
        // Sort the input array into default ascending order
        Arrays.sort(nums);
        int prev = nums[0];
        int count = 0;
        // Recording a number already a repeated one
        // e.g [9,9,9] the 3rd 9 will not increase duplicate count again
        boolean numAlreadyRepeated = false;
        for(int i = 1; i < nums.length; i++) {
            if(prev == nums[i] && !numAlreadyRepeated) {
                count++;
                numAlreadyRepeated = true;
            } else if(prev != nums[i]) {
                prev = nums[i];
                numAlreadyRepeated = false;
            }
        }
        return count;
    }
    
    0 讨论(0)
  • 2020-12-06 03:42

    Here I have written code in JAVA. also the inputted numbers, have been considered as String. This question has also been added to CODEWARS. and I hope this simple solution helps You

    public class countingduplicates {
      public static void main(String[] args) {
        int i=0,j=0,c=0,a=0;
        String text="7261474547731";
        text=text.toLowerCase();
    
        for(i=0; i<text.length(); i++) {
          for(j=0; j<text.length(); j++) {
            if(text.charAt(i) == text.charAt(j)) { 
              c++;
            }
          }
    
          System.out.println(text.charAt(i) + " occured " + c + " times");
          if(c>1) {
            a++;
          }  
    
          String d = String.valueOf(text.charAt(i)).trim();
          text = text.replaceAll(d,"");
          c = 0;
          i = 0; //cause i have trimmed the string and by default i increases by 1, so i have to assign it =0
          j = 0; //cause i have trimmed the string and by default j increases by 1, so i have to assign it =0
        }
      System.out.println("Total count of Duplicates:" + a);
      }
    }
    
    0 讨论(0)
提交回复
热议问题