title: Most common array element in tableView
body: <p>I would like to begin with saying that I am a beginner at coding in iOS (Swift 2.0). So I have an array which I want to sort with the most most common element first and the least common last. I then want to insert this into a UITableView where the most common item should be displayed at the top and the least common in the bottom. So if i have an item called "Food" in the array and it appears 3 times it should be on top of an item called "Candy" that appears 2 times. Thanks!</p>
  
  <p>EDIT:
  I'm sorry for being providing a such a bad question, and as i said, I'm a beginner at programming so I'm not the best at explaining my situation either. But I will try to describe my situation better this time. Firstly, I wanted to sort my array with the most common element first, and the second most common element after that and so forth. The answer that was provided by John helped me do that, and after that i managed to put it into a tableView like this: </p>
  
  <pre><code> var counts : Dictionary&lt;String, Int&gt; = Dictionary&lt;String, Int&gt;()
  
  
  override func viewDidLoad() {
  
      for (_, value) in prioLista.enumerate() {
  
          if let count = counts[value] {
  
              counts[value] = count + 1
  
          }
          else {
              counts[value] = 1
          }
  
      }
  
      prioLista.sortInPlace { (first, second) -&gt; Bool in
  
          let firstCount = counts[first]
          let secondCount = counts[second]
  
          return firstCount &gt; secondCount
      }
  
    navigationItem.title = "Spara"
  
  }
  
  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return prioLista.count
  }
  
  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
      let cellster = tableView.dequeueReusableCellWithIdentifier("cellster")
  
  
      cellster?.textLabel?.text = prioLista[indexPath.row]
  
      return cellster!
  }
  </code></pre>
  
  <p>The variable prioLista that is included in the code is an array from another file. By pressing a button you can add Strings into it.</p>
  
  <p><a href="http://i.stack.imgur.com/810Vu.png" rel="nofollow">My tableView after sorting elements after most common</a></p>
  
  <p>So this is where I am now, as the image views. The tableView sorts the elements just as I wanted. However, I would like to have only ONE row per different object. So if the array contains more "Mat" than "Godis", I still want "Mat" to be above "Godis", but I want them to take up one row each. As of now the tableView shows every single item I have in my array, and instead I would like it to remove duplicates, but still prioritize the most common ones to be on top of the less common ones. I know it might sound weird that i want to remove the items but still use the function provided by John, but if there is a way of doing this I would be so happy. </p>
  
  <p>So now to summarize what I actually want so I make this as clear as possible; In the picture that I linked, my objects are sorted by how common they are, and i wonder if there is a way of only displaying one of the same objects but still have the array sorted in this way. So if there are more "Mat" than "Godis" it should be displayed as:</p>
  
  <p>first row: Mat second row: Godis</p>
  
  <p>in the tableView</p>
  
  <p>I hope that I were clear enough and just as a reminder, I am just a beginner who's trying to learn to code :)</p>