skip to Main Content

How to sort an integer array based on a duplicate values count. here less number of duplicates should come first.

input  [5, 2, 1, 2, 4, 4, 1, 1, 2, 3, 3, 6]
OutPut [5, 6, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1]

2

Answers


  1. Chosen as BEST ANSWER
    let numbers = [5,2,1,2,4,4,1,1,2,3,3,6]
    let sortedNumber = numbers.sorted()
    print("Input: ",sortedNumber)
    
    var dict = [Int: Int]()
    for item in sortedNumber {
        let isExist = dict.contains(where: {$0.key == item})
        if !isExist {
            dict[item] = 1
        } else {
            if let value = dict[item] {
                dict[item] = value + 1
            }
        }
    }
    
    var finalArray = [Int]()
    let sortedArray = dict.sorted { (first, second) -> Bool in
        return first.value < second.value
    }
    
    for d in sortedArray {
        for _ in 1...d.value {
            finalArray.append(d.key)
        }
    }
    print("Output: ",finalArray)
    
    Input:  [1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 6]
    Output:  [5, 6, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1]
    

  2. Using Martin’s comment, here is another approach which aims to reduce the number of loops and conditions we write ourselves by using some functions provided by swift.

    // Input
    let numbers = [1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 6]
    
    // Count the occurrences based on Martin's comment
    let countDict = numbers.reduce(into: [:], { $0[$1, default: 0] += 1 } )
    
    // Get a sorted array of the countDict keys, sorted by value which
    // is the number of occurrences
    let sortedKeys
        = countDict.keys
        .sorted { countDict[$0, default: 0] < countDict[$1, default: 0] }
    
    // Initialize an empty array to hold the final sorted numbers
    var sortedNumbers: [Int] = []
    
    // Add the elements into the sortedNumbers with in their desired order
    for key in sortedKeys {
        sortedNumbers.append(contentsOf: repeatElement(key,
                                                       count: countDict[key, default: 0]))
    }
    
    // prints [5, 6, 4, 4, 3, 3, 1, 1, 1, 2, 2, 2] based on the above input
    print(sortedNumbers)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search